开发者

Virtual method but non-virtual destructor

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜