使用一个对象后它的析构函数被调用 [英] using an object after it's destructor is called
本文介绍了使用一个对象后它的析构函数被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?
可以访问一个局部变量的内存
代码:
#include <iostream>
using namespace std;
class B{
public:
int b;
B():b(1){}
~B(){cout << "Destructor ~B() " << endl;}
};
class A{
public:
B ob;
A()try{throw 4;}
catch(...){cout << "Catched in A() handler : ob.b= " << ob.b<<endl;}
};
int main()try{
A t;
}
catch(...){cout << "CATCHED in Main" << endl;}
输出: b
$ b
Output:
Destructor ~B()
Catched in A() handler : ob.b= 1
CATCHED in Main
我的问题是如何访问成员变量 b
My Question is how it's possible to access a member variable b
of an object ob
that its destructor call finished.
推荐答案
因为对象而对象已被破坏,它占据的实际内存仍然存在。但是,这是未定义的行为,可能会或可能不工作。
Because while the object has been destroyed, the actual memory that it occupied still exists. However, it's undefined behavior, and may or may not work.
这篇关于使用一个对象后它的析构函数被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文