Ok, i see some things have been confused here and it has to do with the way the variables are declared... The example of vinayvaka will work as will many of the above examples (even though they didn't work for BahatiSiD). So, why?
Or... although the example of vinayvaka works as it is, if BahatiSiD tries it with his variables, it won't, i guarantee you that.
Well, BahatiSiD have declared his variables on the heap, hence they're not local variables allocated on the stack. You may ask what does it have to do with all this, but remember that this is MC++ and not ANSI C++. So, you can't pass the variables that are on the heap the same way as the local variables. The problem is that in a managed environment, the address of a variable allocated on the heap might change (and will change during the lifetime of a program numerous times, hence it is not safe to use it that way).
I could write this function:
-
void Swap(int& x, int& y)
-
{
-
int temp = x;
-
x = y;
-
y = temp;
-
}
-
and using textBoxes as in BahatiSiD's example call it this way:
-
int num1 = 15;
-
int num2 = 30;
-
-
textBox->AppendText("--------------------" + Environment::NewLine);
-
textBox->AppendText("BEFORE x = " + num1.ToString() + ", y = " + num2.ToString() + Environment::NewLine);
-
Swap(num1, num2);
-
textBox->AppendText("AFTER x = " + num1.ToString() + ", y = " + num2.ToString() + Environment::NewLine);
-
and it will work like a charm. But, if i call it this way:
-
textBox->AppendText("--------------------" + Environment::NewLine);
-
textBox->AppendText("BEFORE x = " + var1.ToString() + ", y = " + var2.ToString() + Environment::NewLine);
-
Swap(var1, var2);
-
textBox->AppendText("AFTER x = " + var1.ToString() + ", y = " + var2.ToString() + Environment::NewLine);
-
I'll get an error:
error C2664: 'TestProject::Form1::Swap' : cannot convert parameter 1 from 'int' to 'int &'
Note that i just used BahatiSiD's variables as he has declared them. I am making this example so that BahatiSiD can try it out in his sample project.
So, what the heck...
We're telling the compiler that Swap() takes 2 int&, and it's fine, but why in the first example the compiler passed our variables by-reference, and the second time it did not?
As i previously said, you cannot use ordinary native C++ pointers with the garbage-collected heap because if the location of the data pointed to changes, the pointer will no longer be valid.
So, what we need now is a way to access the object on the heap that enables the address to be updated when the gc will reallocate the data item on the heap. We have 2 ways at our disposal:
1. Tracking handle (or simply, handle), which is analogous to a pointer in native C++
2. Tracking reference (equivalent to a reference in native C++)
So... they act pretty much as their counterparts in native c++ (as much as the programmer might be concerned, although he should understand the fundamental differences, because they really are different), but don't forget the keyword TRACKING. They track the address of the object on the heap, so you always have a valid pointer/address.
That's why the examples with interior_ptr<int> worked, because interior_ptr<Type> is managed by the CLR and the address is accordingly updated whenever the GC collects.
So, in order to make it work, you may use interior_ptr<int>, or if you want to use the forementioned Swap() function (or any other function of that type) and make it work, you may "cheat" a little bit this way:
-
int num1 = var1;
-
int num2 = var2;
-
-
textBox->AppendText("--------------------" + Environment::NewLine);
-
textBox->AppendText("BEFORE x = " + num1.ToString() + ", y = " + num2.ToString() + Environment::NewLine);
-
Swap(num1, num2);
-
var1 = num1;
-
var2 = num2;
-
textBox->AppendText("AFTER x = " + num1.ToString() + ", y = " + num2.ToString() + Environment::NewLine);
-
What we do here is, we allocate 2 int variables on the stack, initialize them with the values of the heap variables, swap them and store the resulting values back to the heap variables.
I wouldn't go in details here about the tracking types (maybe in some other thread) as the post is large as it is without it, but i hope you understand what were you doing wrong in your approach and how to work around the problem.
Cheers