Virtual method but not virtual destructor

I get the error "The Polygon class has a virtual 'area' method, but not a virtual destructor" in the Eclipse CDT. What for? Code snippet:

Header Files:

class Shape { public: virtual ~Shape(); protected: virtual double area() const = 0; } class Polygon : public Shape { public: ~Polygon(); protected: double area() const; private: Vertex* vertices; } 

Implementation:

 Polygon::~Polygon() {delete[] this->vertices;} double Polygon::area() const { ... return areaSum; } 
+6
source share
2 answers

It sounds like an error in an eclipse, or maybe it’s a β€œstyle” of warning of a minor problem. The polygon does have a virtual destructor automatically, because the base class destructor is virtual.

+7
source

Try the following:

 class Shape { public: virtual ~Shape() {} protected: virtual double area() const = 0; } class Polygon : public Shape { public: virtual ~Polygon(); protected: double area() const; private: Vertex* vertices; } 

This works for me, for the problem!

-2
source

Source: https://habr.com/ru/post/895910/


All Articles