Tuesday, July 14, 2009

Assigning pointers in C++?

I do not understand the following code





V * v = new V;


v-%26gt;i=8;


System::Console::WriteLine(v-%26gt;i);





pin_ptr%26lt;V%26gt; mv = %26amp;*v; %26lt;--- what does %26amp;*v means?


mv-%26gt;i = 7;

Assigning pointers in C++?
It appears that you took this example right from the MSDN documentation. Basically what this code is doing is declaring a pointer to the structure 'V', then it is setting the integer member 'i' to the value of 8.





The pin_ptr%26lt;V%26gt; mv = %26amp;*v; line is what creates a "pinned" pointer to the boxed value type. The "%26amp;*v" says to assign the address to the pinned pointer of the data which is pointed to by the pointer 'v'.





You can then change the value through the pinned pointer and your structure's member will be changed as well.
Reply:V *v=new V;


Creating a pointer object v, of a Class V.





v-%26gt;i=8;


Assigning value 8 to member variable i of object v.





%26amp;v is the address of pointer v.

surveys

No comments:

Post a Comment