Does the ^ symbol replace C#'s "ref" in parameter passing in C++/CLI code?
If Dog
is a reference type (class
in C#) then the C++/CLI equivalent is:
void MyFunction(Dog^% dog)
If Dog
is a value type (struct
in C#) then the C++/CLI equivalent is:
void MyFunction(Dog% dog)
As a type decorator, ^
roughly correlates to *
in C++, and %
roughly correlates to &
in C++.
As a unary operator, you typically still need to use *
in C++/CLI where you use *
in C++, but you typically need to use %
in C++/CLI where you use &
in C++.
The ^ operator behaves similarly to a pointer in C++/CLI. The difference is that it's a garbage-collected pointer. So:
Dog ^ mydog = gcnew Dog();
is simply saying that we will new using the managed memory (gcnew) and pass the managed pointer back to mydog.
So:
void MyFunction(Dog ^ dog)
Is actually passing by address, not be reference, but they're kinda similar. If you want to pass by reference in C/C++ you do something like:
void MyFunction(Dog &dog);
in the function declaration. I assume it'll be the same for C++/CLI, but I've never tried it. I try not to use the ref's since it's not always clear that they are.
EDIT: Well, it's not the same, it's % not &, which makes sense they'd have to change that too. Stupid C++/CLI.