Teis said:
I want to write a base class that includes a member function that creates an
instance of a derrived class and returns a pointer to it.
I posted a very similar problem to the moderated newsgroup and got an
excellent reply. Firstly, it's best to note that the abstract factory
pattern might be more appropriate for what you want (I don't know your
situation). It's something to look into.
This is the trick, originally posted to the moderated group by Maxim
Yegorushkin (altered a little by me):
class Base
{
public:
Base( Derivation ) { ... }
template< class T >
T Create() { return T( Derivation() ); }
// To return a boost::shared_ptr, replace the above with:
// template< class T >
// boost::shared_ptr< T > Create() { return new T( Derivation ); }
protected:
Base() { ... }
struct Derivation {};
private:
// You might want copy ctors and copy assignment operators here.
};
class Derived : public Base
{
public:
Derived( Base:
erivation ) { ... }
protected:
Derived() { ... }
};
Derived can now only be created like this:
Derived d = Base::Create< Derived >();
Any other attempt to create it will cause a compiler error. It works
because the constructor that is used by Create is public, but cannot be
accessed outside the hierarchy because the parameter is a protected
member of Base.
Issues:
1) Two constructors required per class. This is not strictly true, but
if you don't provide a protected default constructor, the public
constructor must pass its parameter back to its base class' constructor.
2) If you leave out the public constructor in a class, but keep the
protected default constructor, you will have a class that cannot be
created (but classes derived from it can still be created). This is
useful when you want this functionality but can't provide a pure virtual
method.
3) Think carefully about what you want to return from Create. As it
stands the object is returned by value (which means the copy-constructor
and copy assignment operator can still be used to construct a new
object). This might be fine for you, but if not you can either make them
private or return a smart pointer instead (such as boost::shared_ptr,
from
www.boost.org).
I will say this once more -- you would do well to look into abstract
factory and other design patterns/techniques before using this. Your
choice, though.
Oh, and I don't guarantee that the above code compiles, obviously.
-- Pete