Make a templated class with a member that is a pointer to the specified type. In that templated class, make a templated member function (that's 2 layers of templating) which deletes the previous child instance and dynamically allocates a new instance of the specified type (make sure you have a virtual destructor on the base class).
Then, make a single linked list of that type (with the corresponding base class as the template parameter), and when you want to set the child type, call the templated member function.
Voila!
Quick example of a class similar to what I'm describing
Code:
template< typename BaseType >
class PolymorphismHandler
{
public:
PolymorphismHandler()
: DynamicInstance( 0 ) {}
~PolymorphismHandler() { delete DynamicInstance; }
template< typename ChildType >
ChildType& SetType()
{
delete DynamicInstance;
return *dynamic_cast< ChildType* >( DynamicInstance = new ChildType );
}
BaseType& operator()() { return *DynamicInstance; }
const BaseType& operator()() const { return *DynamicInstance; }
protected:
BaseType* DynamicInstance;
};
And then make the list out of those
IE
Code:
SinglyLinkedList< PolymorphismHandler< MyBaseClass > > MyList;
Link& CurrentLink = MyList.AddLink();
CurrentLink.Data().SetType< MyChildType >();
That of course all depends on if you have a good compiler. MSVC++ 6.0 for example, chokes on the above code despite the fact that it is completely valid C++ code.