Is it possible to set the address of a reference outside of the constructor?
worksCode:int& a = b;
Obviously just changes the value, not the reference.Code:int& a;
a = b;
Printable View
Is it possible to set the address of a reference outside of the constructor?
worksCode:int& a = b;
Obviously just changes the value, not the reference.Code:int& a;
a = b;
Do you have something like?
You'll need to use the constructors initializer list:Code:class N
{
int& a;
public:
N(int& b) {???}
};
Code:N(int& b): a(b) {}
Does that work outside of constructors, such as a method? Like this:
EDIT: after a test, it turns out i HAVE to do that constructor initializer list thing for every object reference in the class. I guess its because its unsafe to have an unitialized object reference..?Code:class ClassCache
{
private:
ScriptManager& scriptManager;
..
..
public:
..
..
void Assign(ScriptManager& scriptM): scriptManager(scriptM){}
};
The language forbids you from having uninitialized references.
If you need to change what the reference is referencing, you can always use pointers.
Yeah ill just leave it as a pointer. I can always do * to convert it to a reference or value.