Just a little confusion I am hoping someone can help clear up...
I have a method passing in a handle to a Point structure to which I make changes to X and Y values. On return to the caller I find that the Point values are no different. The declareation is Point ^pPt
Now if I instead use Point% pPt for passing a reference then any change to pPt.X or pPt.Y works.
I thought I could treat Point ^pPt similar to a pointer where changes to pPt->X, pPt->Y would be seen by the calling code
Thanx

C++/CLI confusion about % vs. ^
tchris38
LozD
MethodA(SomeClass ^pClassVar)
for efficency should change to
MethodA(SomeClass% pClassVar)
Only thing I do not like is from C# the code now would need an explicit ref declare in passing pClassVar
What is the recommended approach
Sebbie85
Willem Termans
news:1da63330-a8ea-434e-a1d5-ddc03bf61ca5@discussions.microsoft.com
> I have a method passing in a handle to a Point structure to which I
> make changes to X and Y values. On return to the caller I find that
> the Point values are no different. The declareation is Point ^pPt
> Now if I instead use Point% pPt for passing a reference then any
> change to pPt.X or pPt.Y works.
--
With best wishes,
Igor Tandetnik
Atul Sehgal
using
namespace System;value
struct Point {int x;
int y;
};
void
f1(Point^ p){
p->x += 2;
p->y += 2;
}
void
f2(Point% p){
p.x += 2;
p.y += 2;
}
int
main(){
Point p = { 1, 2 };
Console::WriteLine(
"p.x = {0}, p.y = {1}", p.x, p.y);f1(p);
Console::WriteLine(
"p.x = {0}, p.y = {1}", p.x, p.y);f2(p);
Console::WriteLine(
"p.x = {0}, p.y = {1}", p.x, p.y);}
If compiled and ran this program will produce:
p.x = 1, p.y = 2
p.x = 1, p.y = 2
p.x = 3, p.y = 4
The issue here is that when you box a value-type (which is what happens when f1 is called in the example above) the runtime creates a copy of your value-type on the GC heap: so any modifications to this copy are not reflected in the orginial instance of the value-type.
In general if you wish to modify a value-type don't box it instead pass it by reference.
Mark Jordan
In my example above I call function, f1, with a instance of the Point class p: the compiler sees that the function requires a P^ so it emits the instructions to CLR to box p and then it passes the handle to this boxed P to the function. Think of it as follows in Standard C++;
struct Point {
int x;
int y;
};
void f(P* pPoint)
{
pPoint->x += 2;
pPoint->y += 2;
}
P p = { 1, 2 };
P tmp = p;
f(&tmp);
In this case here f will update the contents of the variable tmp and not of the original instance p.
The only way to do this using a pointer is to initially create a boxed value-type:
Point^ p = new Point(1, 2);
f1(p);
But I really cannot recommend this solution as it has its own problems: the major one being that C++/CLI is the only language that can manipulated boxed value-types like this.
Tom Schulte
In C++ a most efficient manner of passing a class instance was via a pointer to that class instance. In C++/CLI what is the most efficient manner of passing a class instance around Is it reference or handle
THanx.