Dynamic down cast to abstract class (C++)

12,630

Solution 1

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.

Solution 2

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.

Share:
12,630
tim_a
Author by

tim_a

Languages: Fluent: Java, HTML, Javascript, CSS Learning: Various webtechnologies: jQuery, AngularJS, .Net framework (C#), Node.js Moderate: PHP, C, C++

Updated on June 04, 2022

Comments

  • tim_a
    tim_a almost 2 years

    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.

    • keyser
      keyser about 10 years
      Does this have anything to do with visual studio? Is it their error? (I'm asking because of the tag)
    • tim_a
      tim_a about 10 years
      I am coding this in visual studio that's all. I get the error while coding but also during compiling (C2683).
    • atoMerz
      atoMerz about 10 years
      Why are you casting Cell to obstacle in a Cell method?
    • YoungJohn
      YoungJohn about 10 years
      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.
    • atoMerz
      atoMerz about 10 years
      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.
    • David Rodríguez - dribeas
      David Rodríguez - dribeas about 10 years
      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.
  • tim_a
    tim_a about 10 years
    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?
  • Neil Kirk
    Neil Kirk about 10 years
    @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.
  • mradul dubey
    mradul dubey over 2 years
    Can you please elaborate on why a virtual destructor is required ?
  • Neil Kirk
    Neil Kirk over 2 years