Destructing derived class by deleting base class

后端 未结 3 435
暗喜
暗喜 2021-01-25 16:39

I have a situation that looks like the following code:

#include 

class A
{
public:
    A() { std::cout << \"A created!\" << std::end         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-25 17:07

    If you apply operator "delete" to base class pointer, destructor MUST be virtual (existence of other virtual methods does not matter). For instance, in case of multiple inheritance "delete" operator applied to base class pointer will cause memory fault since compiler doesn't even know were the memory occupied by derived object begins.

提交回复
热议问题