Monday, 19 August 2013

Calling an object-specific function on a STL vector of polymorphic objects

Calling an object-specific function on a STL vector of polymorphic objects

So I have a class hierarchy that essentially has Entity class as the
parent abstract class and a bunch of other classes that derive from it,
such as Door, Player, Ground, etc.
I also have a three-dimensional vector that stores the pointers to objects
of type Entity and I fill up this vector with the derived objects. Within
the Door class I have a function called isOpen() that simply returns a
boolean. This function is specific to the Door class and is neither found
in Entity class nor in any other derivations of it (as I don't need to
check whether, for example, a Ground object is open or not).
Now, knowing that there exists an object of type Door at vector position
i, j, k, I would like to call the function isOpen like so:
vector[i][j][k]->isOpen(). Unfortunately, when I do this, the compiler
returns 'class Entity has no member named isOpen()'. This is
understandable since the function isOpen() is exclusive to the Door class,
but what can I do in order to make this sort of call possible?
Thanks for your time.

No comments:

Post a Comment