Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to initialize member reference with temp object and delete the temp object when the destructor is called?

Reply
Thread Tools

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

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      05-08-2005
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);
}

 
Reply With Quote
 
 
 
 
Samee Zahur
Guest
Posts: n/a
 
      05-09-2005
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

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      05-09-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments