【C++】virtual method but non-virtual destructor

本文最后更新于:2022年5月29日 上午

有虚函数(纯虚函数)就应该有虚析构函数。

1
2
3
4
5
6
7
8
9
10
11
class Element
{
public:
virtual ~Element()
{
//
}

virtual bool read() = 0;
virtual bool write() = 0;
};

如果没有虚析构函数,使用该类声明指针,然后在删除该指针的时候会出现下面警告:

1
warning: delete called on 'xxxx' that is abstract but has non-virtual destructor

解决方法:

在类补充虚析构函数即可消除警告。


【C++】virtual method but non-virtual destructor
https://kevinloongc.github.io/posts/58c3d271.html
作者
Kevin Loongc
发布于
2021年3月18日
许可协议