8

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.

tim_a
  • 940
  • 1
  • 7
  • 20
  • Does this have anything to do with visual studio? Is it their error? (I'm asking because of the tag) – keyser Apr 15 '14 at 16:35
  • I am coding this in visual studio that's all. I get the error while coding but also during compiling (C2683). – tim_a Apr 15 '14 at 16:37
  • 1
    Why are you casting Cell to obstacle in a Cell method? – atoMerz Apr 15 '14 at 16:37
  • 3
    This seems like a convoluted way to solve this problem. If `isAccessible` were a virtual method then `Obstacle` could override it and simply return false and this would no longer be an issue. – YoungJohn Apr 15 '14 at 16:39
  • 1
    I don't know what you're trying to do, but the error is because your Cell class is not polymorphic i.e. it does not have any virtual methods. If you add a virtual method to Cell your code should compile. – atoMerz Apr 15 '14 at 16:43
  • 1
    Using `instanceof` in Java or `dynamic_cast` to 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. – David Rodríguez - dribeas Apr 15 '14 at 17:06

2 Answers2

17

Cell class must have at least one virtual function to use dynamic_cast. Also, if Cell is your base class, it should have a virtual destructor.

You should make isAccessible a virtual function and override it in Obstacle to return false.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
4

What you're doing is wrong. Generally you shouldn't need to cast to a sub type of a class in its base class. If you need it, it is likely a design error. In your case the code should look like this.

virtual bool Cell:: isAccessible()
{
  return true;
}

bool Obstacle::isAccessible()
{
  return false;
}

P.S. The cause of your error is that Cell class does not have a virtual method and thus it does not show polymorphic behaviour.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • Thanks. However apart from the fact I was trying to solve the problem the wrong way (by casting) why should Cell always have at least one virtual method in order to be able to use a dynamic_cast? – tim_a Apr 15 '14 at 16:50
  • @tim_a dynamic_cast and virtual functions need some information to be embedded in the object, but this increases its size slightly, so C++ lets you choose whether to add the information. – Neil Kirk Apr 15 '14 at 16:54