Why simple destructor does not delete the derived object if declared using base pointer

后端 未结 4 1935
清酒与你
清酒与你 2021-01-25 12:11
int main()
{

    Base *p = new Derived;
    delete p;
    return 0;
}

I have some confusion, why deleting p here won\'t delete the derived object ? Is

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-25 12:23

    Deletion of an object via a pointer to its base class is undefined unless the base has a virtual destructor. Even with a virtual destructor, the order of deletion may not be what immediately comes to mind.

    For more information, see this destructor reference.

提交回复
热议问题