12

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.

6
  • Does this have anything to do with visual studio? Is it their error? (I'm asking because of the tag) Commented Apr 15, 2014 at 16:35
  • 1
    Why are you casting Cell to obstacle in a Cell method? Commented Apr 15, 2014 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. Commented Apr 15, 2014 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. Commented Apr 15, 2014 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. Commented Apr 15, 2014 at 17:06

2 Answers 2

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please elaborate on why a virtual destructor is required ?
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.

2 Comments

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 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.