By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,316 Members | 2,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,316 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Apr 21 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.