在 C++ 编译期间,'有虚拟方法......但非虚拟析构函数'警告是什么意思?

2022-01-24 00:00:00 polymorphism c++ virtual
#include <iostream>
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    virtual int area ()
      { return (0); }
  };

class CRectangle: public CPolygon {
  public:
    int area () { return (width * height); }
  };

有编译警告

Class '[C@1a9e0f7' has virtual method 'area' but non-virtual destructor

如何理解这个警告以及如何改进代码?

How to understand this warning and how to improve the code?

这个版本现在正确吗?(试图给出答案以阐明自己的概念)

is this version correct now? (Trying to give answer to elucidate myself with the concept)

#include <iostream>
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    virtual ~CPolygon(){};
    virtual int area ()
      { return (0); }
  };

class CRectangle: public CPolygon {
  public:
    int area () { return (width * height); }
    ~CRectangle(){}
  };

推荐答案

如果一个类有虚方法,那意味着你希望其他类继承它.这些类可以通过基类引用或指针来销毁,但这仅在基类具有虚拟析构函数时才有效.如果你有一个应该可以多态使用的类,那么它也应该可以多态删除.

If a class has a virtual method, that means you want other classes to inherit from it. These classes could be destroyed through a base-class-reference or pointer, but this would only work if the base-class has a virtual destructor. If you have a class that is supposed to be usable polymorphically, it should also be deletable polymorphically.

这里也深入回答了这个问题.下面是一个完整的示例程序,演示效果:

This question is also answered in depth here. The following is a complete example program that demonstrates the effect:

#include <iostream>

class FooBase {
public:
    ~FooBase() { std::cout << "Destructor of FooBase" << std::endl; }
};

class Foo : public FooBase {
public:
    ~Foo() { std::cout << "Destructor of Foo" << std::endl; }
};

class BarBase {
public:
    virtual ~BarBase() { std::cout << "Destructor of BarBase" << std::endl; }
};

class Bar : public BarBase {
public:
    ~Bar() { std::cout << "Destructor of Bar" << std::endl; }
};

int main() {
    FooBase * foo = new Foo;
    delete foo; // deletes only FooBase-part of Foo-object;

    BarBase * bar = new Bar;
    delete bar; // deletes complete object
}

输出:

Destructor of FooBase
Destructor of Bar
Destructor of BarBase

注意 delete bar; 导致析构函数 ~Bar~BarBase 被调用,而 delete foo; 只调用 ~FooBase.后者甚至是未定义行为,因此无法保证效果.

Note that delete bar; causes both destructors, ~Bar and ~BarBase, to be called, while delete foo; only calls ~FooBase. The latter is even undefined behavior, so that effect is not guaranteed.

相关文章