Peter Oehlert
Peter Oehlert

Reputation: 16958

What does the C++/CLI Object^% (caret percent-sign) declaration mean?

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.

Upvotes: 24

Views: 11224

Answers (5)

% 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.

Upvotes: 19

Reed Copsey
Reed Copsey

Reputation: 564811

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

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942257

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.

Upvotes: 19

Steven Behnke
Steven Behnke

Reputation: 3344

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)

Upvotes: 3

dan04
dan04

Reputation: 91207

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

Upvotes: 10

Related Questions