R
raylopez99
Please comment on the following code fragment. Note the "WHY"?
Particularly, (1) why does referencing and dereferencing a pointer give
the same thing (at least in WriteLine(), which might be some sort of
cast going on behind the scenes), (2) why doesn't the pointer "stick
to" a reference, such as when y = intRef; below, then intRef is changed
but the y handle does not change, and (3) why doesn't y = &intVT
compile?
Thanks
RL
///////////////////////////////////////////////////////////////
// in managed C++/CLI console mode
int intVT = 10;
int %intRef = intVT;
int ^y = gcnew int(200);
Console::WriteLine("Should be 200 {0} EITHER WAY !: {1} ", y, *y);
*y = 210; //also works y = 210; //WHY?
Console::WriteLine("Should be 210 {0}...is it? (yes)",*y);
y = intRef;
Console::WriteLine("Should be intRef or 10: {0}...is it (Yes)?",y);
intRef = 21;
Console::WriteLine("Should be intRef or 21: {0} , {1}...is it
(NO)?",y, intRef); //WHY NOT?
y = intRef; //but now it should work: add-- y = intRef; // also works:
*y = intRef; // same answer of 21 //WHY?
Console::WriteLine("Should be intRef or 21: {0} , {1}...is it
(Yes)?",y, intRef);
//y = &intVT; //unsafe operation by definition? (no, illegal!)
//won't compile! WHY?
Particularly, (1) why does referencing and dereferencing a pointer give
the same thing (at least in WriteLine(), which might be some sort of
cast going on behind the scenes), (2) why doesn't the pointer "stick
to" a reference, such as when y = intRef; below, then intRef is changed
but the y handle does not change, and (3) why doesn't y = &intVT
compile?
Thanks
RL
///////////////////////////////////////////////////////////////
// in managed C++/CLI console mode
int intVT = 10;
int %intRef = intVT;
int ^y = gcnew int(200);
Console::WriteLine("Should be 200 {0} EITHER WAY !: {1} ", y, *y);
*y = 210; //also works y = 210; //WHY?
Console::WriteLine("Should be 210 {0}...is it? (yes)",*y);
y = intRef;
Console::WriteLine("Should be intRef or 10: {0}...is it (Yes)?",y);
intRef = 21;
Console::WriteLine("Should be intRef or 21: {0} , {1}...is it
(NO)?",y, intRef); //WHY NOT?
y = intRef; //but now it should work: add-- y = intRef; // also works:
*y = intRef; // same answer of 21 //WHY?
Console::WriteLine("Should be intRef or 21: {0} , {1}...is it
(Yes)?",y, intRef);
//y = &intVT; //unsafe operation by definition? (no, illegal!)
//won't compile! WHY?