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

P

PengYu.UT

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);
}
 
S

Samee Zahur

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
 
M

Mark P

Samee said:
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
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top