为什么在这个类中没有调用析构函数
why destructor is not called in this class
我有一个简单的类,在test.h
class test
{
test()
{
std::cout<<"constructor called"<<std::endl;
}
static test m_test;
~test()
{
std::cout<<"I am here"<<std::endl;
}
};
, static成员在test.cpp中定义为:
test test::m_test;
main没有任何内容:
main()
{
}
,我可以看到输出:
constructor called
I am here
是好的。现在我添加一段代码,生成一个像这样的异常:
main()
{
for(int i=-1; i<1; i++)
{
i=1/i; // this line generate an exception and close the application.
}
}
在这种情况下,不调用析构函数。我只能看到构造函数被调用了。为什么会这样?
如何确保在抛出期望并且应用程序崩溃时调用析构函数?假设我只能更改我的测试类而不能更改主应用程序
除零也不例外!这是不可能捕捉到的,因为它是一个硬件信号,操作系统中断您的程序。
阅读更多:
c++:捕获除零错误要抛出随机异常,只需执行
throw std::runtime_error("Oh no!");
,但要确保在调用代码中捕获抛出的异常:
try {
codeThatThrowsException();
} catch(const std::runtime_error& e) {
std::cout << "An exception was thrown: " << e.what() << std::endl;
}
相关文章:
- 什么时候调用析构函数
- C++-明确何时以及如何调用析构函数
- C++ 防止在映射中放置()时调用析构函数
- 调用析构函数以释放动态分配的内存
- C++:使用方法调用析构函数的顺序是什么?
- 向量推回调用析构函数时调用析构函数
- 如何在调用析构函数时优雅地停止/销毁带有阻塞调用C++线程?
- C++,我应该调用析构函数吗?
- 如何获取有关在 Clang LibTooling 中调用析构函数的信息?
- 当我从 std::vector 中的新放置调用析构函数时会发生什么?
- 为什么这里不调用析构函数
- 在调用 std::bind 的产品后意外调用析构函数
- 为什么在传递给函数而不是构造函数时调用析构函数?
- 如何在C++中调用析构函数
- 为什么为未删除的对象调用析构函数?
- 调用析构函数时出错
- C++ 在不释放内存的情况下调用析构函数
- 为什么在运算符删除中不调用析构函数?
- C++ 调用析构函数后动态模板队列"double free or corruption (out)"
- 在 postOrderDelete 上调用析构函数时引发的异常