What's with the new C++/CLI pointers (handles ^)? Managed C++.NET

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?
 
K

kwikius

raylopez99 said:
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?

Technically this isnt standard C++, but OTOH I personally find the
garbage collected types in C++/CLI interesting. I would guess that you
cant get the address because being a handle this gives the
implementation the fredom to change the actual address whenever it
wants, in order to move gc stuff in memory or whatever. You could do
the same type of stuff in standard C++ I guess with some handle<T>
type...

Anyway you are probably better off to refer your questions to a more
appropriate newsgroup, where they can give more details.

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top