greg phillip
greg phillip

Reputation: 161

copy constructor copy pointer to abstract class

Say I have a class General, which holds a pointer to an abstract class, *_abstract. If I want to implement General copy constructor, how is it done?

I try this but it fails:

General::General(const General &other)
{
    *_abstract = *other._abstract;
}

I also tried:

General::General(const General &other)
{
    *_abstract = new Abstract();
    *_abstract = *other._abstract;
}

which is impossible because of the abstract class initialization (no constructor)

Upvotes: 0

Views: 156

Answers (1)

Pete
Pete

Reputation: 4812

If you need to deep copy the abstract class then add a virtual clone function to the abstract class and copy it with that.

understanding virtual copy constructors

Upvotes: 1

Related Questions