change variable to reference even if it was not declared as reference
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 dec开发者_运维技巧lare 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
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.
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.
精彩评论