当我使用dynamic_cast并删除对象删除时,析构函数是如何工作的?
When I use dynamic_cast and delete the object delete , how the destructor works?
我为它创建一个派生类的对象和一个基类点, 然后我使用dynamic_cast将基类点转换为派生类点。 我删除了派生类点,但程序调用派生析构函数和基析构函数。为什么程序调用基析构函数?毕竟,基本析构函数不是一个虚函数。
#include<iostream>
#include<stdlib.h>
using namespace std;
class con {
private:
double num;
public:
con() {
num = 0;
cout << "default..." << endl;
}
void getnum() {
cout << num << endl;
}
};
class base {
public:
virtual void A() {
cout << "it is base A" << endl;
}
void B() {
cout << "it is base B" << endl;
}
~base() {
cout << "it is base decon" << endl;
}
};
class child : public base {
public:
void A() {
cout << "it is child A" << endl;
}
void B() {
cout << "it is child B" << endl;
}
~child() {
cout << "it is child decon" << endl;
}
};
int main(int argc, char** argv) {
base* b = new child();
child* c = dynamic_cast<child*>(b);
delete c; //the program print "it is child decon" "it is base decon"
getchar();
return 0;
}
情况 1:如果删除指针派生类,它始终首先调用派生类的析构函数,然后调用基类的析构函数。
情况 2:如果使用非虚拟基类析构函数删除指向派生类的基类的指针,则它不知道派生类,因此它只会删除基类。
情况 3:如果使用虚拟基类析构函数删除指向派生类的基类的指针,它将使用虚拟调度到派生类的析构函数,然后调用基类的析构函数。
案例 2 很糟糕。 它可能导致资源泄漏等。 应通过在旨在作为基类的类中始终声明析构函数虚拟来避免这种情况。
您的示例说明了案例 1。
如果您想查看案例 2 的实际效果,请尝试delete b
而不是示例中的delete c
。
C++中的析构函数不会"重载";它们会链接起来。当你说
{
child c;
}
然后在右大括号处,编译器将插入对~child()
的调用,而 又会调用~base()
(以及其他基类的析构函数,如果有的话(。这对于避免内存泄漏至关重要——否则base
的成员将如何被摧毁?请注意,这不需要virtual
base
析构函数。
当您使用delete
手动销毁某些内容时,这的工作方式相同:
child* c = new child{};
delete c; // runs ~child(), which calls ~base()
同样,这不需要virtual
析构函数。
虚拟析构函数所做的是允许您使用指向base
的指针销毁动态类型child
变量,即
child* c = new child{}
base* b = c;
delete b; // Will call ~child() if destructor is virtual, otherwise ~base()
相关文章:
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 编译"运算符删除"时C++编译器如何工作?
- 从列表向量中删除无法按预期工作
- C++自定义删除运算符不能正常工作?
- 双向链表 std::unique_ptr 类在节点删除时无法按预期工作
- 擦除删除成语的性能增益从何而来
- 当删除 [] 指针工作时,为什么无法获得指向的数组大小?
- 当我使用dynamic_cast并删除对象删除时,析构函数是如何工作的?
- "= 删除"如何工作?有人可以解释这个构造函数吗?
- 删除和删除[]在内部如何工作?
- 删除运算符如何在我的代码中工作
- 在删除另一个类中的一个类的对象时析构函数如何工作
- C :删除对象(使用新的),但成员函数仍然可以正常工作(?)
- 当 std::unique_ptr 在手动删除其拥有的对象后超出范围时,它如何工作?
- C++放置位置删除如何在内部(C++运行时)工作?如何克服其局限性?
- 更改C 的返回指针后可以删除工作
- 隐式删除的默认构造函数在 C++14 中工作
- 对运算符删除覆盖的工作方式感到困惑
- 新建和删除如何工作以及它们的存储位置
- 对象在删除后工作