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.