R
raylopez99
microsoft.public.dotnet.languages.vc
Please comment on the following code fragment. Note the comments
"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, using Visual Studio 2005, C++.NET
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 y, *y same?
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 same answer (why y, *y same)?
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 won't compile?
// EOF
/////////////////////////////////////////////////////////////////////////////////
Please comment on the following code fragment. Note the comments
"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, using Visual Studio 2005, C++.NET
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 y, *y same?
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 same answer (why y, *y same)?
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 won't compile?
// EOF
/////////////////////////////////////////////////////////////////////////////////