# 2 Variables, 1 Address?

Printable View

• 01-02-2004
Epo
2 Variables, 1 Address?
Hullo hullo :)

I was wondering if this is okay to do:
Code:

```void Function2(int &MainX) {         int X;         &X = MainX } void Function1(void) {         int X = 2;         Function2(X); }```
And then use X in both functions as one in the same. So if in Function2 I change X to 5, it will change the X in Function1 to 5 as well (and vice versa).

Is that okay to do?
• 01-02-2004
Salem
It would if you did
Code:

```void Function2(int &X) {         X = 5; }```
• 01-02-2004
Epo
Hmm, I know I'm just complicating things, but is there a way to do this with three variables like my first example?

The reason is that I'm using classes and I'm trying to set private variables within those classes able to alter the initial variables. I'll complicate the code a bit:
Code:

```class MyClass { public:         MyClass(int &MainX, int &MainY, int &MainZ);         ~MyClass(void); private:         int X, Y;         int *Z; }; MyClass::MyClass(int &MainX, int &MainY, int &MainZ) {         &X = MainX;         &Y = MainY;         &Z = MainZ;         X = 5;         Y = 2;         Z = 7; } void Function1(void) {         int X, Y;         int *Z;         MyClass MyVariable(X, Y, *Z); }```
So, if Function1 runs first, would the final values of X, Y, and Z not equal 5, 2, and 7 respectively? (Z must remain a pointer) :)

(Based on your response Salem, I'm assuming not :) But I'm not sure why...)

Thanks
• 01-02-2004
Epo
What are memory addresses stored as? I think I may have a solution to this...

Of which type of variable are they? (Float, Double, etc.)
• 01-02-2004
thefroggy
Code:

`&X = MainX;`
Did you even try to compile what you've posted? That isn't even a valid expression: you can't change the address of variables.

If you want to change those values, you can return a reference:
Code:

```class myclass { public:   int& get_x() { return x; } private:   int x; }; int main() {   myclass m;   int& x = m.get_x();   x = 10; // m.x will now be 10 }```
• 01-02-2004
XSquared
It's a bad idea to return a reference from a function, because the memory is not available outside the function.
• 01-02-2004
jlou
Quote:

Originally posted by XSquared
It's a bad idea to return a reference from a function, because the memory is not available outside the function.
This is only true if the reference is to a variable local to that function. In this case, the reference is to a member variable that is valid as long as the object itself is valid. Of course, there could still be problems with it if the object goes out of scope, but in the example provided it is fine.
• 01-02-2004
jlou
I'd do something like this (when you say Z must remain a pointer, do you mean MyClass::Z or the Z inside Function1?):
Code:

```class MyClass { public:         MyClass(int &MainX, int &MainY, int &MainZ);         ~MyClass() { } private:         int& X;         int& Y;         int* Z; }; MyClass::MyClass(int &MainX, int &MainY, int &MainZ) :   X(MainX), Y(MainY), Z(&MainZ) {         X = 5;         Y = 2;         *Z = 7; } void Function1() {         int X, Y;         int *Z = new int;         MyClass MyVariable(X, Y, *Z);         delete Z; }```
So now your class has references to X and Y. (By the way, references must be initialized in the initialization list instead of inside the constructor, which is why I added that). You also have a pointer to Z. Whenever you change X, Y or *Z inside your class, the variables passed into them will get the new values.

Of course, this is dangerous, since once the function ends and X, Y and Z are destroyed, any time MyClass tries to access them you will get undefined behavior and possibly a crash.

Also, its hard to tell if you understand between '&' the address of operator and '&' meaning reference, and '*' the dereference operator and '*' meaning pointer. If you don't, you might want to read up on that before continuing with whatever you are trying to do.