ko*****@gmail.com wrote:
Rolf Magnus wrote: Naren wrote:
... A *a = new A;
int & r = a.get(); // int& A::get() { //returning a member of
class A}
delete a;
cout<<r;
That would invoke undefined behavior.
And what if I use
int const& r = a.get(); //int const& A::get()
will it still be undefined behavior?
What's different here? 'a' is not a temporary, A::get returns
a reference, not a temporary. As soon as 'a' is destroyed, all
of its contents is gone, and any reference to any part of 'a'
becomes invalid. Using it has UB.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask