C++/CLI:为什么我不能通过引用传递字符串?
为什么 Microsoft 的 C++/CLI 不允许我通过引用传递字符串?我收到以下错误:
C3699:“&”:无法在“System::String”类型上使用此间接寻址
Why doesn't Microsoft's C++/CLI allow me to pass strings by reference? I received the following error:
C3699: '&': cannot use this indirection on type 'System::String'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,.NET 实际上有两种 Microsoft 特定的 C++ 方言:较旧的“托管 C++”(Visual Studio 2002 和 2003)和 C++/CLI(Visual Studio 2005 及更高版本)。
在 C++/CLI 中,
System::String^
是对字符串的 .NET 引用;一些作者将其称为“跟踪指针”,以将其与普通的 C++ 指针进行比较和对比。与在 C++ 中一样,您可以“按引用”传递 .NET 引用,但不使用&
,而是使用%
,如下所示:First of all, there are really two Microsoft-specific C++ dialects for .NET: the older "Managed C++" (Visual Studio 2002 and 2003) and C++/CLI (Visual Studio 2005 and later).
In C++/CLI,
System::String^
is a .NET reference to a string; some authors call this a "tracking pointer" to compare and contrast it with a normal C++ pointer. As in C++, you can pass .NET references "by reference", but instead of using&
, you use%
, as in:听起来您正在使用托管 C++,这是与 .NET Framework 一起使用的劣质 C++。
在托管 C++ 中,我相信您正在寻找的语法是
System::String^
。原因是,由于托管类型是由 .NET Framework 进行垃圾收集的,因此不允许您创建“常规”引用,因为 GC 需要跟踪对特定变量的所有引用,以了解何时可以安全地释放它。Sounds like you are using Managed C++, which is a bastardised C++ used with the .NET Framework.
in Managed C++, I believe the syntax you are looking for is
System::String^
. The reason for this is that since managed types are garbage collected by .NET Framework, you aren't allowed to create 'regular' references since the GC needs to track all the references to a specific variable to know when it is safe to free it.看起来您正在使用托管 C++。您应该使用
System::String^
来代替。It looks like you are using Managed C++. You should use
System::String^
instead.