2

I am just curious about C++/CLI handle(^) and how it works. For e.g:

The first thing that I cannot understand is, over here the accepted answer says that the caret is the managed equivalent of a * (pointer). However, unlike *, I can directly assign value to ^.

int ^num;
num = 4;

This would not work in C++:

int *num2;
num2 = new int[1];
num2 = 10;

Why is this behavior observed? Also, in Microsoft Webpage they say you cannot point to a member of the object, and it does not support pointer arithmetic. So both the following code snippet would fail to compile

int ^num3 = gcnew int[10];

for(int i = 0; i<10; i++)
    %(num3 + i) = i

or

for(int i = 0; i<10; i++)
    (num3 + i) = i

Why???

That brings me to final question: What would be equivalent of following int native/normal C++ code snippet in C++/CLI?

int *array1;
array1 = new int[ 10];

for( int i = 0; i < 10; i++)
     *(array1 + i ) = i;
Community
  • 1
  • 1
Dwight Schrute
  • 359
  • 1
  • 5
  • 17

2 Answers2

4

int^ means a reference to a boxed integer.
It's not a pointer to a regular integer.

A handle to a reference type in CLR isn't equivalent to a pointer. It is similar.

Both "point" at an object, but that's about where the similarity ends.
It's much more like a C++ shared_ptr.

Anyway, a boxed integer is an object that wraps an integer number.
int^ is a a reference to a boxed object, so besides holding a value, it can be null (because it's a reference).

when you do int^ = 4; the boxed object's constructor construct a boxed object with the value of 4.
So unlike a c++ pointer, you're not pointing a the address "4" you're "pointing" at an object that happens to contain the value 4.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • Can you please explain me the difference between boxed integer and regular integer? – Dwight Schrute Jan 12 '13 at 21:20
  • @Dwight : Microsoft paid people to write documentation, SO is made up of volunteers – read the documentation _first_ before asking the unpaid people to take time explaining something that is already readily available. ;-] [Boxing (C++/CLI)](http://msdn.microsoft.com/en-us/library/hh875061.aspx) – ildjarn Jan 12 '13 at 21:37
  • @Dwight : I've added some explanation. Read the links anyway. – Yochai Timmer Jan 12 '13 at 21:47
  • @Yochai Timmer: Thanks for comprehensive explanation for boxed integer – Dwight Schrute Jan 14 '13 at 18:08
1

^ is the equivalent of *, for the purposes of dealing with class objects (new/gcnew, passing as parameters, storing as part of a class definition, etc). Pointer arithmetic isn't the same in C++/CLI.

Here's the equivalent to your code snippet:

array<int>^ array1;
array1 = gcnew array<int>(10);

for (int i = 0; i < 10; i++)
    array1[i] = i;

array<int>^: This is a managed reference, stored on the stack.

array1 = gcnew array<int>(10): This is creating a managed object (from the class named "array") on the managed heap, and assigning the reference to the local variable on the stack.

array1[i] = i: Behind the scenes, this calls a method on the "array" class, passing it the index and the new value (both i in this case).

David Yaw
  • 27,383
  • 4
  • 60
  • 93