warning: deleting object of abstract/polymorphi class type which has non-virtual destructor

warning: deleting object of abstract class type 'XXXX' which has non-virtual destructor will cause undefined behaviour [-Wdelete-non-virtual-dtor]|

warning: deleting object of polymorphic class type which has non_virtual destructor

如果基類裏有虛函數,然後定義了基類指針指向派生類,就需要定義基類的虛析構函數,這樣,基類指針析構的時候,就會首先析構派生類,再析構基類。

 

在用基類指針指向派生類時,

在基類析構函數聲明爲virtual的時候,delete基類指針,會先調用派生類的析構函數,再調用基類的析構函數。

在基類析構函數沒有聲明爲virtual的時候,delete基類指針,只會調用基類的析構函數,而不會調用派生類的析構函數,這樣會造成銷燬對象的不完全。

 

c++虛析構函數的必要性: https://www.cnblogs.com/Trojan00/p/8907736.html

詳解C++中虛析構函數的作用及其原理分析:  https://www.jb51.net/article/159253.htm

使用受保護的非虛擬析構函數時,禁止使用delete-non-virtual-dtor警告(Suppress delete-non-virtual-dtor warning when using a protected non-virtual destructor):  https://www.it1352.com/782718.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章