c – “删除”是否与多态性一起正常工作?

c – “删除”是否与多态性一起正常工作?,第1张

概述参见英文答案 > Virtual destructor and undefined behavior                                    4个 BaseClass * p = new DerivedClass();delete p; 我知道第二行将调用基类的析构函数,如果它没有虚拟析构函数和派生类的析构函数,如果它有,但会正确删除内存(让我们说BaseClass 参见英文答案 > Virtual destructor and undefined behavior                                    4个

BaseClass * p = new DerivedClass();delete p;

我知道第二行将调用基类的析构函数,如果它没有虚拟析构函数和派生类的析构函数,如果它有,但会正确删除内存(让我们说BaseClass的对象占用8个字节的空间和DerivedClass的一个12 – 它将释放8或12个字节)并在任何一种情况下摆脱对象?

解决方法 好吧,如果它有一个虚拟析构函数,当然会破坏对象并按预期释放内存.如果它没有虚拟析构函数,则行为未定义.

if the static type of the object to be deleted is different from its dynamic type,the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

因此,试图判断内存是否将被完全解除分配并没有任何意义.程序可以用内存做任何喜欢的事情.

总结

以上是内存溢出为你收集整理的c – “删除”是否与多态性一起正常工作?全部内容,希望文章能够帮你解决c – “删除”是否与多态性一起正常工作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1215049.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存