Both Derived class new_object functions call clone. (They look exactly the same.)Code:template<typename T> T* clone(const T& t) { return new T(t); } //... class Base { //this is an abstract base class virtual Base* new_object() const =0; } class Derived : public Base { virtual Base* new_object() const; } class Derived2 : public Derived { virtual Base* new_object() const; } Base* Derived::new_object() const { return clone(*this); }
I've got 3 questions
1) If I did not repeat the definition or the declaration for Derived2, it would call Derived's function. *this would be a const Derived&, not a const Derived2& which I want. Is this correct?
2) What if I just declared a virtual function in the header file for Derived2, but left the definition to Derived?
3) What if I declared new_object in Base? (Of course, I would take away the '=0' part of it).
Basically, I'm looking to see if there's a way to let the right typename be chosen so that clone() doesn't slice the object.