Boost logo

Boost Users :

From: Andy Rushton (quergle-mingw_at_[hidden])
Date: 2008-07-05 07:33:57

John wrote:
> This pattern/idiom is well-established:
> struct A {
> virtual A* clone () = 0;
> };
> struct B : public A {
> virtual B* clone ();
> };

Except you've quoted it wrong. The return type of virtual functions must
always be the same for all classes in the polymorphic hierarchy. This is
standard C++ polymorphism - always use a pointer to the superclass to
point to all subclasses. The return type in this case should be A* for
both cases:

   struct A {
       virtual A* clone () = 0;

   struct B : public A {
       virtual A* clone ();

Similarly, for the shared_ptr version, do the same, i.e. always use
shared_ptr<A> to point to objects of types A and B.

See the manual for my own smart pointer classes for more on this topic:

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at