C++ maintaining a mixed collection of subclass obj

2019-07-19 07:05发布

问题:

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;
    ...
    ...
}

回答1:

You can define abstract Clone method which will be implemented in each subclass.

virtual BaseClass* Clone() = 0;

Than just call:

for (ItemCollectionType::const_iterator it = Items.begin(); it != it_end; ++it) 
{
    BaseClass *src = it->second;
    NewItems[item->code] = src->Clone();
}

Also note that C++ support return value covariance so your derived classes can return the exact type and not just the base class, e.g.

// in DerivedClass (which derives from BaseClass)
DerivedClass* Clone() { ... } 

This is considered a proper override, although the return type is not the same as in the base class.

Casting the base class pointers to derived classes, based on an attribute, is not considered good OOP practice.



回答2:

Create a new function in your subclasses (perhaps make it pure virtual in your base class) that is something along the lines of,

BaseClass * SubClassA::copy(){ /* Copy construtor like code. */ }

This will invoke dynamic binding when you call BaseClass->copy() and call a copy constructor on the SubClass.