24

This apparently is a Google-proof term since I can't get any search engines to not throw away the "extra" characters. I did also look on MSDN in the C++ reference but I can't seem to find the C++/CLI reference because there is nothing in the declarations section on it.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Peter Oehlert
  • 16,368
  • 6
  • 44
  • 48
  • 2
    MSDN: http://msdn.microsoft.com/en-us/library/8903062a%28VS.80%29.aspx See also: http://stackoverflow.com/questions/202463/what-does-the-caret-mean-in-c-cli – BlueRaja - Danny Pflughoeft Feb 18 '11 at 00:35
  • @BlueRaja, if you answer it I'll mark it as right. I just found that link and the extra CLI section in the docs. Thanks. – Peter Oehlert Feb 18 '11 at 00:37
  • possible duplicate of [what does the symbol ^% mean in c++ project](http://stackoverflow.com/questions/4999575/what-does-the-symbol-mean-in-c-project) – Ben Voigt Feb 18 '11 at 00:45
  • 1
    The MSDN link in @BlueRaja-DannyPflughoeft 's comment above is now broken. Here's a valid archived version from the Wayback Machine: https://web.archive.org/web/20121129100839/http://msdn.microsoft.com/en-us/library/8903062a(VS.80).aspx – AJM Mar 02 '21 at 16:17

5 Answers5

19

% is a tracking reference.

It is similar to a native reference (Object&), but a tracking reference can reference a CLR object while a native reference cannot. The distinction is necessary because the garbage collector can move CLR objects around, so a CLR-object's memory address may change.

The ^ is a handle. This simply means it is managed. See MSDN and also this SO post.

AJM
  • 1,317
  • 2
  • 15
  • 30
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
19

It means "pass by reference":

 void bar::foo(Object^% arg) {
    arg = gcnew Object;    // Callers argument gets updated
 }

Same thing in C++:

 void foo(Object** arg) {
    *arg = new Object;
 }

or C#:

 void foo(out object arg) {
     arg = new Object();
 }

C++/CLI doesn't distinguish between ref and out, it does not have the definite assignment checking feature that the C# language has so no need to distinguish between the two. Same in VB.NET, ByRef vs ByVal.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
10

Essentially, it's the "managed" version of Object*&, and equivalent to ref or out on a reference type in C#.

dan04
  • 87,747
  • 23
  • 163
  • 198
3

This is a managed pointer by reference. So if you had something like:

void DoSomething(System::String^% stringObject)

in C# it would look like:

void DoSomething(ref System.String stringObject)
IS4
  • 11,945
  • 2
  • 47
  • 86
Steven Behnke
  • 3,336
  • 3
  • 26
  • 34
0

This is a C++/CLI Tracking Reference. This is kind of like a C++ reference, but to a managed object.

AJM
  • 1,317
  • 2
  • 15
  • 30
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373