-4

I tried to convert by "dynamic_cast" in the following way:

#include <iostream>

class Shape {
    //....
};

class Square: Shape {
    //....
};

class Circle: Shape {
    //....
};

int main() {
    Circle cr;
    Shape* sh = &cr; // ERROR1
    Square* psq = dynamic_cast<Square*>(sh); //ERROR2
    return 0;
}

And I get error messages:

ERROR1: 'Shape' is an inaccessible base of 'Circle'

ERROR2: cannot dynamic_cast 'sh' (of type 'class Shape*') to type 'class Square*' (source type is not polymorphic)

Can someone explain why I get these errors?

Mathing
  • 41
  • 5
  • 3
    Possible duplicate of [Difference between private, public, and protected inheritance](https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) –  Mar 17 '18 at 18:36
  • You are inheriting privately from class `shape`. You should make it `public`: `class Square : public Shape`... – Raindrop7 Mar 17 '18 at 18:37
  • @Raindrop7 But still I get `ERROR2` – Mathing Mar 17 '18 at 18:38
  • @NickyC I agree that it's related to `ERROR1` , but what with `ERROR2` ? – Mathing Mar 17 '18 at 18:42
  • 1
    It is related to code that you do not show us. –  Mar 17 '18 at 18:43
  • @NickyC That is all my code.. – Mathing Mar 17 '18 at 18:44
  • Then, it is literally what it say: The class is not polymorphic. –  Mar 17 '18 at 18:45
  • @NickyC And what is the meaning of that? – Mathing Mar 17 '18 at 18:46
  • Another duplicate: https://stackoverflow.com/questions/15114093/getting-source-type-is-not-polymorphic-when-trying-to-use-dynamic-cast –  Mar 17 '18 at 18:47
  • @NickyC Why I need from the beginning to make `Shape` as polymorphic? What is the problem now? – Mathing Mar 17 '18 at 18:50
  • 1
    Because you are using it as a polymorphic class. The problem is you are using a non-polymorphic class as a polymorphic class, which is a contradiction. –  Mar 17 '18 at 18:54
  • @nickyc Where it's seen in my code that I using it as a polymorphic class? – Mathing Mar 17 '18 at 18:55
  • You must make the class polymorphic in order to safely downcast it using `dynamic_cast`. It's a stupid rule in C++ that you must have at least one virtual function to make a class polymorphic. If you wanna get around of it, replace `dynamic_cast` by `static_cast`, although the latter does not have "safety check" – SwiftMango Mar 17 '18 at 18:55
  • `dynamic_cast`. –  Mar 17 '18 at 18:56
  • @NickyC So, if I can use `dynamic_cast` just about polymorphic classes? – Mathing Mar 17 '18 at 18:57
  • @texasbruce It's just in case of downcast or that any using in `dynamic_cast` demand it? – Mathing Mar 17 '18 at 18:58
  • I am only referring to your case which is a downcast. To know more about dynamic_cast, refer to the documentation: http://en.cppreference.com/w/cpp/language/dynamic_cast – SwiftMango Mar 17 '18 at 18:59
  • Because you are trying to cast a `Circle` to a `Square`, which will fail and return null pointer. Again, you can use `static_cast`, which is **not safe** and allow you to cast in this case and return non-null. – SwiftMango Mar 17 '18 at 19:12

1 Answers1

2

The first error is that you must inherit publicly from Shape To be able to call the Shape's constructor in the derived object construction.

The second error is because class Shape must be polymorphic which means at least there's a virtual method:

class Shape {
    public:
        virtual ~Shape(){}
    //....
};

class Square: public Shape {
    //....
};

class Circle: public Shape {
};

Circle cr;
Shape* sh = &cr; // ERROR1
Square* psq = dynamic_cast<Square*>(sh);
  • Polymorphism requires pointer to base class and virtual functions and operator overloading.

  • You could use dynamic_cast to cast a derived class to a non polymorphic base class. But you cannot dynamic_cast a non polymorphic base to a derived class.

Eg:

Circle* cr = new Circle;
Shape* shp = dynamic_cast<Shape*>(cr); // upcasting

The lines above works fine even if the base class Shape is not polymorphic.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27