I get the error "Class 'Polygon' has virtual method 'area' but non-virtual destructor" in Eclipse CDT. Why? 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;
}
Sounds like a bug in eclipse, or maybe it's a 'style' warning about a minor issue. Polygon does have a virtual destructor automatically because it's base class destructor is virtual.
Try this:
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 issue faced!
精彩评论