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

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

P: n/a
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);
}

Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.