Hello.
I'm writing a manager class that should handle a vast and expandable group of classes. The manager class uses a hash_map to store all these etherogenous objects. The objects are all derivations of a base class Object, and of different "interface" classes which are used to define required functions. An example:
class Object{ ... };
class IUpdatable
{
...
public:
virtual void Update()=0;
...
};
So I could have classes that derive from class Object and from class IUpdatable, but that override the Update function in very different ways - or that do not even derive from IUpdatable (meaning that particular type of object requires no update at all after creation).
Now, I can't change this design. So, first I thought to just fill the hash map with pointers to Object, and (continuing with the example above) within the manager Update method, let the polymorphism kick in once I call the Update function of the updatable stored objects, like this:
void Update()
{
// SHM is the hash map type, s is the hash map instance
for (SHM::iterator i=s.begin(); i != s.end(); ++i)
{
i->second->Update();
}
}
But obviously I can't do that, since
1. I do not know if the stored object derives from IUpdatable too.
2. Update is not a member of Object (and it makes no sense to have Update defined in Object).
I was thinking that I could just use typeid and confront the type info with all the possible types that derive from that "interface" class, but that's ugly programming imho. Maybe it's possible to check if a class implements a function (and in that case I'd automatically be allowed to cast the object to the IUpdatable type and use polymorphism).
So my question is, is there an elegant way to solve this situation?