本文介绍了在调用析构函数后使用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

#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;}

输出:

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.

推荐答案

因为它的析构函数调用完成了一个对象 ob 当对象被破坏时,它占用的实际内存仍然存在。但是,这是未定义的行为,可能或可能无法正常工作。

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.

这篇关于在调用析构函数后使用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 04:47