By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Dec 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

raylopez99 wrote:
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

Dec 18 '06 #2

P: n/a

kwikius wrote:
raylopez99 wrote:
Anyway you are probably better off to refer your questions to a more
appropriate newsgroup, where they can give more details.
Oops sorry didnt realise it was crossposted.

Dec 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.