Say I have the following C++ class:
namespace shapes {
// Simply a base class with virtual destructor
class DLL Polygon {
public:
Polygon();
virtual ~Polygon();
virtual string Type();
string _type;
virtual double getArea()=0;
};
class DLL Rectangle : public virtual Polygon { // virtual inheritance
public:
double area;
Rectangle(double x0, double y0, double x1, double y1);
~Rectangle();
double getLength(); // Add method that does not exist in Base class
string Type(); // override base function
double getArea(); // implement virtual method
};
Since Rectangle inherits virtual Polygon, in C++, I would need to do a dynamic cast
to access the methods for the Rectangle class. See example below:
Polygon * polygon = new Rectangle(1,2,3,4);
Rectangle * rect;
rect = dynamic_cast<Rectangle *>(polygon); // dynamic cast
cout << polygon->Type() << endl;
cout << "area: "<< polygon->getArea() << endl;
cout << rect->getLength() << endl;
I know cython supports the following for casting:
cdef Polygon * polygon
cdef Rectangle * rect
polygon = new Rectangle(x0,y0,x1,y1)
rect = <Rectangle *>polygon // static cast
However, since Rectangle does virtual inheritance of Polygon, I get a compiler error:
Rectangle/cy_rectangle.cpp:1557: error: cannot convert from base ‘shapes::Polygon’ to derived type ‘shapes::Rectangle’ via virtual base ‘shapes::Polygon’
Can I do a dynamic_cast in Cython?
Thanks,
Jasmine