Why the standard doesn't force following the "simple" references link ?

X

X Ryl

For example, let's say I have such classes :
struct G
{
int value;
G(int a) : value(a) {}
~G() { cout<< value <<" - G is Destructed!!!"<<endl; value = 0; }
};

struct B
{
G a1;
virtual ~B() { cout<<"B is Destructed!!!"<<endl; }
B(G a) : a1(a) {}
};

struct C : public B
{
G a2;
C(G a, G b) : B(a), a2(b) {}
virtual ~C() { cout<<"C is Destructed!!!"<<endl; }
};

struct A
{
const B & ref;
A & coutCoco() { cout << "Coco" << endl; return *this;}

A(const B& _ref) : ref(_ref) {}
};

and in the code I simply does this :
A a(C(3, 2));

// Use a...
cout<<"End was here"<<endl;

For what I understand from the standard, the output should be:
G is destructed (this one is for the temporary "3" or "2" because
argument are evaluated at compiler choice)
G is destructed (this one is for the other temporary)
C is destructed (this is for the C temporary)
G is destructed (for the local C's a2 member)
B is destructed (again for the temporary C)
G is destructed (for local B's a1 member)
End was here

However, in such an easy example, the temporary C object is no more
reachable after the "a" definition, so the "ref" member of "a" refers
to a destructed object.
Why doesn't the standard force the temporary C object to live until "a"
is destructed, in such an "easy" to spot case ?

BTW, the standard ensure that if I had called the stuff like:
A y = A(C(3,2)).coutCoco();
the temporaries A, C are still reachable(not destructed) when using the
coutCoco method. y's "ref" is not however reachable after that line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top