Re: Clone an object with an abstract base class
On 11/10/2014 22:46, Urs Thuermann wrote:
I want to clone an object through a pointer to its abstract base
class. Say I have the following classes
class B {
virtual void foo() = 0; // abstract base class
};
class D1 : public B {
virtual void foo();
};
class D2 : public B {
virtual void foo();
};
Then I can clone objects when I know the subclass of B
void f1(D1 *d1, D2 *d2) {
B *b1 = new D1(*d1);
B *b2 = new D2(*d2);
}
but not if I only have a pointer to the abstract base class
void f2(B *b) {
B *b1 = new B(*b); // error: cannot create a B
}
With GCC and -std=c++11 I can write
void f2(B *b) {
B *b1 = new auto(*b);
}
with my GCC and c++11 on does not work:
error: cannot allocate an object of abstract type 'B'
Is this the correct and preferred way in C++11?
In C++98 the only way I found is to add a pure virtual function
B *B::clone() const
and
B *D1::close() const { return new D1(*this); }
B *D2::close() const { return new D2(*this); }
and then call b->clone() instead of the new operator.
Is there a way to do this in C++98 without the need to add a new
function like clone() in each derived class?
urs