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

Discussion in 'C++' started by X Ryl, Apr 21, 2006.

  1. X Ryl

    X Ryl Guest

    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.
     
    X Ryl, Apr 21, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rado
    Replies:
    24
    Views:
    774
    kayodeok
    Jan 3, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,270
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    1
    Views:
    359
    red floyd
    Jan 1, 2007
  4. grocery_stocker
    Replies:
    2
    Views:
    482
    Terry Reedy
    Apr 5, 2009
  5. Jake Barnes
    Replies:
    1
    Views:
    181
Loading...

Share This Page