views:

139

answers:

4

How to bound 2 variables in C++ one with other so when one changes changes another?

for example I created Int A and Int B bound one to another and than when I change A one using some function another one will automatically change to new value of A.

I am intrested in version for C++ .net 4th version.

+11  A: 

I don't know about the .Net version of C++, but you can use references in C++ to do what you want:

int  A = 0;
int& B = A;
...
A = 10;  // B == 10
B = 100; // A == 100
AraK
+5  A: 

You can't do it with vanilla values, there needs to be indirection somewhere.

You could refer to one:

int a;
int& b = a;

a = 5;
assert(b == 5);

Point to one:

int a;
int* b = &a; // b points to a

a = 5;
assert(*b == 5);

Or create some utility for essentially wrapping the above.

GMan
A: 

You give up on the idea of doing so with fundamental types and make A an observable.

Noah Roberts
what does observable mean here?
Blender
I think he means that one should use the observer pattern. In this case, a reference or a pointer seems to be what the OP was asking for, which can certainly be done with a fundamental type.
Matthew Hall
We don't know what the OP was asking for, they never answered the question posed about that very thing. Can B be different from A at any time? If so then references won't do. Further, references and pointers seemed a bit too basic to have absolutely no idea they existed.
Noah Roberts
This is actually a good answer if you don't assume the OP is *completely* clueless about references and tells them how to get exactly what they asked for: notification that a value has changed so that another can respond to it. Don't know why it got so many negs, but whatever.
Noah Roberts
+2  A: 

Hi Ole,

How to bind 2 variables in C++ one with another so when one changes changes another?

You are looking for a concept called a reference, which is built on the concept called a pointer. The actual concept is that you want two Thingies pointing to a memory location.

for example I created Int A and Int B bound one to another and than when I change A one using some function another one will automatically change to new value of A.

You mean B will change to the new value of A, I take it.

int &B = A; creates the reference B that points to the location A refers to.

Another approach - not the best approach for a beginner - is:

int *A = new int;  //Get some heap memory for A to point to
int *B = A;        

That uses pointers.

I am interested in version for C++ .net 4th version.

Strictly speaking, C++ is an ISO standard; .NET doesn't have anything to do with C++. However, Microsoft has the C++/CLI language which is sorta C++, but really isn't.

Paul Nathan
Actually, the concept "reference" isn't _built_ on the concept "pointer". A "reference" is, conceptually, an _alias_ to another object. A "pointer" is the other object's _address_. However, all known C++ compilers implement them both of in the same way (and pointers were there first) so people tend to think the "reference" concept just copied the "pointer" concept and changed it a little.
sbi
@sbi: I suppose so, in the denotational semantics frame of thought. I'm sort of leery of conceptualizing machine-level concepts that don't actually exist in the machine, so I really do think that a reference is a (non-arithmetic) pointer.
Paul Nathan