c++ 2 ref classes should have acess to one same object of a other class

69 Views Asked by At

I have 2 classes let's say class a und b. And even a thrid class c.

class a and b should have acess to the functions of the same object of class c. I want to have a member like a reference. So the argument in the construktor of class a and b would be the object or something like that.

Because I'm new to the CLI syntax im kinda confused with the datatype reference class.


so a normal example wihtout CLI and classes would like that.

        int c = 5; 
        int &a = c; 
        int &b  = c;

so whenever I change the value of a or b it changes the value of c.

I'm not a native english speaker so please be friendly :3.

1

There are 1 best solutions below

4
On BEST ANSWER

If your class of c is a ref class, you can use ^ (handle) to reference it.

like the code here

 ref_class_c ^ d(gcnew ref_class_c);

 ref_class ^ e = d;

As for the tracking reference versus a handle, the difference is similar to a reference/out parameter in C# method versus the variable name.

For example, try the following code by yourself

 void test1(String^ s) {
     s = gcnew String("no change");
 }
 void test2(String^% s) {
     s = gcnew String("change");
 }

when you call these two functions, you will notice the difference.

 void test3(String^ s) {
     s = "new string 1";
 }
 void test4(String^% s) {
     s = "new string 2";
 }

In your case, either will work.