Apologies if I'm missing a fairly fundamental concept here, but I'm trying to work out how to maintain a collection of multiple class types (all derived from the same parent) and still have access to their subclass-specific methods when retrieving them from the collection.
As context, I have one base class ("BaseClass") and a number of classes (say "SubClassA" through "SubClassZ") that all derive from this base class. I also need to maintain a central indexed collection of all SubClass objects, which I have created as
typedef unordered_map<std::string, BaseClass*> ItemCollectionType;
ItemCollectionType Items;
The list will only ever contain SubClass objects but I've defined the list member type as a "BaseClass" pointer so that it can store instances of any sub class. So far, I'm assuming there's nothing particularly bad about this approach (but please correct me if I'm wrong).
The issue I have is that sometimes I need to create a copy of these collections and their contents. The (simplified) code I have for this is as follows:
ItemCollectionType CopiedItems;
ItemCollectionType::const_iterator it_end = Items.end();
for (ItemCollectionType::const_iterator it = Items.begin(); it != it_end; ++it)
{
BaseClass *src = it->second;
BaseClass *item = new BaseClass(src);
NewItems[item->code] = item;
}
The obvious issue here is that the compiler will only call the BaseClass copy constructor, rather than that of the SubClass that "src" actually points to, because it does not know what type of SubClass it is.
What is the recommended approach for a situation like this? Should I even be in a situation where I "lose" this sub class type information by adding to a collection of type BaseClass? I can't see any other way to maintain the collection but please let me know of any better alternative.
Each SubClass object does contain a numeric ID that indicates which flavour of sub class it is; as a result, is it possible to write a function that translates this numeric ID into a "type" object, which can then be used to cast the source object before calling the copy constructor? Is this a valid use for templating?
Of course, this could be terrible programming practice and/or not possible. I'm just certain there should be a better option than the simplest and least-maintainable solution of
switch (src->code)
{
case SubClassTypes::VarietyA:
SubClassA *item = new SubClassA( *((SubClassA*)src) );
Items[item->code] = item;
break;
case SubClassTypes::VarietyB:
SubClassB *item = new SubClassB( *((SubClassB*)src) );
Items[item->code] = item;
break;
...
...
}