I'm building a simple game design for a project of mine. I have the following classes:
class Character
{
public:
virtual void Display();
virtual void SetParameters( char* param, ... );
};
class NonPlayableCharacter : public Character
{
public:
virtual void Display();
virtual void SetParameters( char* paaram, ... );
int GetNPCState();
}
And then I have a bunch of classes that derive either from Character or NonPlayableCharacter. I define it like so:
std::vector<Character*> _allChar;
My problem is that at any given time I would want to perform some operation on one of the element of the vector. So getting an element out of the vector I can't directly call the method GetNPCState()
because the element in the vector are of Character* type. So doing this:
_allChar[0]->GetNPCState();
doesn't work. So I tried doing it with the famous dynamic_cast:
NonPlayableCharacter* test = dynamic_cast<NonPlayableCharacter*>(_allChar[0]);
test->GetNPCState();
The problem with this last attempt is that GetNPCState()
crashes because the object is null, and I know for a fact (via debugging) that _allChar[0] isn't null.