Reputation: 23
In C++ I can make myReference be a reference of myValue by using & in the declaration of myReference:
int myValue = 123;
int& myReference = myValue;
But if I declare a variable without &, can I later still make it hold the reference to the value of another variable rather than a copy of that value? In other words: Is it possible to change the address of a (non-pointer) variable to the address of another variable, so they both point to the same value? Or is that only possible with pointers?
Cheers, Sadjad
Upvotes: 2
Views: 1006
Reputation: 96243
You can't do this in C++ (a reference must be declared as a reference, and immediately bound to a variable).
If you give us more context on what you are really trying to do here we can offer specific suggestions.
Upvotes: 3
Reputation: 32182
This is not possible. You have to remember that things like types don't exist on the machine-level, so they don't exist during the execution of your program, and variables and references behave very differently.
Upvotes: 2