I am trying to learn some object orientated programming aspect I know from java in C++. However I am having some difficulties in using dynamic_cast where I would use instanceof in Java.
I have a base class Cell and a derived (abstract) class Obstacle. I have defined it like this: Obstacle : public Cell and Obstacle contains a pure virtual destructor. Now in the Cell class I want to implement a method bool Cell::isAccesible(). I've implemented this as follows:
bool Cell::isAccessible() { Obstacle *obs = dynamic_cast<Obstacle*>(this); if (obs != NULL) return false; return true; } However I get the following error back:
"the operand of a runtime dynamic_cast must have a polymorphic class type".
What's wrong with the way I want to implement this? Any guidance is appreciated.
isAccessiblewere a virtual method thenObstaclecould override it and simply return false and this would no longer be an issue.instanceofin Java ordynamic_castto perform type switches in C++ is something you should avoid in the first place. You are creating cyclic dependencies as the base needs to know about the derived and vice versa.