How to initialize member reference with temp object and delete the temp object when the destructor

Discussion in 'C++' started by PengYu.UT@gmail.com, May 8, 2005.

  1. Guest

    Hi,

    There is something wrong with the line labeled "//error". I want D's
    memeber _a point to a "C" object if it is constructed from another "D"
    object. Do you have any idea how to do it? Thanks!

    Best wishes,
    Peng

    class A{
    public:
    A(){}
    virtual ~A(){}
    virtual void fun() = 0;
    };

    class B : public A{
    public:
    B(){}
    virtual ~B(){}
    virtual void fun(){}
    };

    class C : public A{
    public:
    C(A &a){}
    virtual ~C(){}
    virtual void fun(){}
    };

    class D{
    public:
    D(A& a): _a(a) {};
    D(D& d): _a(C(d._a)) {};//error
    D(D& d): _a(d._a) {};//no error
    private:
    A& _a;
    };

    int main(int argc, char *argv[])
    {
    B b;
    D d(b);
    D d1(d);
    }
    , May 8, 2005
    #1
    1. Advertising

  2. Samee Zahur Guest

    Re: How to initialize member reference with temp object and delete the temp object when the destructor is called?

    Humph ... a temporary cannot always be assigned to a reference to a
    non-const object. In this line:

    D(D& d): _a(C(d._a)) {};//error

    The temporary object created probably would get (if such a code was
    allowed) destroyed right after assignment to the reference - meaning
    the reference would be refering to a "destroyed remnants" of the
    object. Hence, it's not allowed. But I don't get it: I saw in a
    previous thread that there is a feature that allows temporaries to have
    an extended life if immediately associated to a reference:

    A& a=C(d._a); //Temporary gets to live as long as the reference a
    does

    So why does this not include case as well? Not sure really. But anyway,
    I can see why you probably do not want the object to be directly a
    member of your class, you can try new/delete ... that will work.

    Samee
    Samee Zahur, May 9, 2005
    #2
    1. Advertising

  3. Mark P Guest

    Re: How to initialize member reference with temp object and deletethe temp object when the destructor is called?

    Samee Zahur wrote:
    > I saw in a
    > previous thread that there is a feature that allows temporaries to have
    > an extended life if immediately associated to a reference:
    >
    > A& a=C(d._a); //Temporary gets to live as long as the reference a
    > does
    >
    > So why does this not include case as well? Not sure really. But anyway,
    > I can see why you probably do not want the object to be directly a
    > member of your class, you can try new/delete ... that will work.
    >
    > Samee
    >


    I may be the blind leading the blind, but here's my guess...

    In your example, A& a and the temporary on the RHS can be created one
    after the other so it's not that hard for the compiler to place them
    adjacent on the stack. This allows them to live for equal durations and
    die together.

    For an object containing a reference this can't be true in general. The
    object may be placed in heap allocated memory, for example, but where
    will the temporary live? Not on the heap, for sure-- no space was
    allocated for it. And the object with a reference may live much longer
    than the current "frame" of the stack, which makes it impossible to
    place the temporary on the stack. Then it requires something like a
    full-blown garbage collector to manage the temporary object.

    -Mark
    Mark P, May 9, 2005
    #3
    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. frs
    Replies:
    20
    Views:
    734
    Alf P. Steinbach
    Sep 21, 2005
  2. Replies:
    3
    Views:
    829
    Peter_Julian
    Oct 10, 2005
  3. arun
    Replies:
    2
    Views:
    533
    benben
    Jun 13, 2006
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    410
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,159
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page