开发者

Virtual method but non-virtual destructor

开发者 https://www.devze.com 2023-03-31 07:47 出处:网络
I get the error \"Class \'Polygon\' has virtual method \'area\' but non-virtual destructor\" in Eclipse CDT. Why? Code snippet:

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

精彩评论

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

关注公众号