I am trying to make a function f that works like this:
StorageObject::addItem (const AbstractBase& base) {
AbstractBase* storage = new Derived1(base);
}
However, I would like f to work with classes Derived2, Derived3 etc as well. Is there a way to get f to call the correct constructor depending on the specific subclass of AbstractBase passed to it, without using typecodes?
To put the question in context, I am using this function to initialize a list of objects in a class. While I could do something like
Derived1* temp = new Derived1();
storageobject.addItem(*temp);
delete temp;
I would prefer to do things like
storageobject.addItem(Derived1());
and have storageobject handle everything.