C++/CLI : Why can't I pass Strings by reference?

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'

Answers


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.


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:

void makeStr(System::String^ %result) {
   result = gcnew System::String("abc");
}

It looks like you are using Managed C++. You should use System::String^ instead.


Need Your Help

What module(s) do I need to include to get 5.seconds to work in Ruby?

ruby datetime libraries

Because apparently require 'date' doesn't include the method hours or seconds etc:

Java Servlets - Storing a list of values in web.xml (multiple param-value's for single param-name)

java web-applications servlets web.xml

I'm creating a servlet that needs to load configuration information. Part of the configuration information I need is a list of Strings (specifically, a list of hostnames and/or URLs).