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

Why doesn't Microsoft C ++ / CLI allow me to pass strings by reference? I got the following error:

C3699: '&': do not use this indirectness in the type "System :: String"

+6
string reference c ++ - cli managed-c ++
source share
3 answers

It looks like you are using Managed C ++, which is the usual C ++ used in the .NET Framework.

in Managed C ++, I believe that the syntax you are looking for is System::String^ . The reason for this is that since managed types are garbage collected by the .NET Framework, you are not allowed to create “regular” links, since the GC must keep track of all the references to a particular variable in order to know when it is safe to free it.

+11
source share

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” for comparison and comparison with a regular C ++ pointer. As in C ++, you can pass .NET links “by reference”, but instead of using & you use % , as in:

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

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

0
source share

All Articles