tags:

views:

274

answers:

3

I'm really baffled by this - I know how to do this in VB, unmanaged C++ and C# but for some reason I can't accept a ref variable of a managed type in C++. I'm sure there's a simple answer, really - but here's the C# equivalent:

myClass.myFunction(ref variableChangedByfunction);

I've tried C++ pointers - no dice. I've tried ref keywords. No dice. I tried the [out] keyword. Didn't work.

I can't find any documentation that clearly explains my problem, either.

Any ideas?

+2  A: 

Use a ^ instead of a *

Joel Coehoorn
I used a ^ - it doesn't appear to pass a reference though. If I modify the contents of the variable they're not reflected in the variable I passed in (according to my watch window at least.)
...Oh man. You mean on the variable I'm passing in.I never even thought to do that despite being well aware of that operator. Lemme try that. Thanks!
+2  A: 

Turns out in the function declaration you need to use a % after the parameter name:

bool Importer::GetBodyChunk(String^% BodyText, String^% ChunkText)

And then you pass in the variable per usual.

A: 

Just to make it a little clearer:

Parameters of reference types (e.g. System::String) have to be denoted with ^ in the newer C++/CLI syntax. This tells the compiler that the parameter is a handle to a GC object.

If you need a tracking reference (like with ref or out in C#) you need to add % as well.

And here comes a tip: I often find it helpful to use the .NET Reflector to look at existing assemblies and switch to C++ code style. This gives good insight into usage of attributes for interoperability between different .net languages.

rotti2