为什么我的程序不崩溃,如果析构函数被调用两次
Why my program does not crash if destructor is called twice
下面的代码是:
#include <iostream>
using namespace std;
class A
{
public:
A() {}
~A()
{
cout << "in destructor" << endl;
}
};
void main()
{
A a;
a.~A();
}
有如下输出:
in destructor
in destructor
为什么我的应用程序不崩溃,如果销毁对象再次销毁?
c++标准,第12.4节[析构函数]
一旦为对象调用析构函数,该对象将不再存在;如果为生命周期已经结束的(3.8)对象调用析构函数,则行为未定义。[示例:如果显式调用自动对象的析构函数,并且随后以通常调用该对象的隐式析构函数的方式留下该块,则该行为是未定义的。
所以你的程序有未定义的行为,它现在可能崩溃,以后,永远不会,地球可能停止旋转等等…别这么做。
注意:
-
void main()
必须是int main()
相关文章:
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- C++析构函数调用两次,堆栈分配的复合对象
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 为什么这个自定义分配器的析构函数在 GCC/MSVS 的 stdlib 中被调用两次
- 重载运算符 new(),为什么构造函数被调用两次?
- 当 reset() 被unique_ptr调用两次时会发生什么?
- CEdit 框的NM_KILLFOCUS被调用两次
- 为什么这个构造函数被调用两次
- 析构函数在与 STL 的共享指针中调用两次
- 构造函数在不同线程中的静态单例类上调用两次
- 类析构函数在创建shared_ptr时调用两次
- 为什么在 C++ 中,当对象包含在另一个对象中时,复制构造函数被调用两次
- makefile 需要调用两次
- [c++]为什么我的类析构函数被调用两次?
- 为什么这个程序中的析构函数被调用两次
- 显式调用析构函数时被调用两次
- C++11 std::generate 和 std::uniform_real_distribution 调用两次会产生
- 基类函数被调用两次
- 为什么析构函数被调用两次,而构造函数只被调用一次
- 为什么函子的 dtor 在作为函数的参数传递给线程时调用两次(多次)