class A{
A(B& ref) : b(ref){}
B getB(){return this->b;}
B b;
};
Does getB() return a reference to A.b or the same reference that was given by the constructor?That would be a problem if the original B has been changed outside of A but A.getB() still returns the old B.
If this would be the case, is there a way to avoid that?
Nope, only a copy of
refis stored in thebmember of the corresponding instance ofA, and a copy of this is what’s returned bygetB.To return the instance of
Bthat the instance ofAwas initialized with (not what you want to do, but ffr), you would do this: