Given the following:
class ParamClass {...};
class MyObject {
public:
void myMethod(ParamClass const& param) { _myPrivate = param; }
private:
ParamClass _myPrivate;
}
[...]
MyObject obj;
void some_function(void)
{
ParamClass p(...);
obj.myMethod(p);
}
What will happen to _myPrivate at the end of the lifetime of object p? EDIT: will I still be able to use _myPrivate to access a copy of object p?
Thanks!
Dan
Since
_myPrivateis not a reference, in the assignment_myPrivate = param, its value will be copied over from whatever the referenceparampoints to, which in this case is the local variablepinsome_function().So if the assignment operator for
ParamClassis implemented correctly, the code should be fine.With the above caveat, yes. But to be precise,
_myPrivatecan not be used to access a copy ofp; it is a variable containing a copy of the data in (the now extinct)p.