警告: deleting object of polymorphic class type which has non_virtual destructor

Posted 风来风往风伤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了警告: deleting object of polymorphic class type which has non_virtual destructor相关的知识,希望对你有一定的参考价值。

如果基类里有虚函数,定义了基类指针指向派生类,就会需要定义基类虚析构,这样,基类指针析构的时候,就会先析构派生类,再析构基类。

 

在用基类指针指向派生类时,

在基类析构函数声明为virtual的时候,delete基类指针,会先调用派生类的析构函数,再调用基类的析构函数。

在基类析构函数没有声明为virtual的时候,delete基类指针,只会调用基类的析构函数,而不会调用派生类的析构函数,这样会造成销毁对象的不完全。

 

以上是关于警告: deleting object of polymorphic class type which has non_virtual destructor的主要内容,如果未能解决你的问题,请参考以下文章