构造函数内的成员析构函数 [英] Member destructor inside a constructor

查看:95
本文介绍了构造函数内的成员析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个A类。还有另一个名为B的班级,看起来像这样的



B级{

私人:

A a;

公开:

B(){a。~A()}

}


请注意,B有一个成员''a'',它是A类的实例。另外

注意到默认值B的构造函数调用''a'的析构函数。


现在让我们在main()块中使用B类:


void main(){

B obj;

}


我的问题是:当这个main()运行时,会发生什么?


好​​吧,我自己编译并运行了这段代码,我发现'a''的
析构函数被调用了两次:首先构造''obj''

(因为B类的构造函数明确地调用''a'的析构函数)和

当控件退出时main()块和''obj''是
destroye d通过调用B类的析构函数。


现在如果''a''已经在B类的构造函数中被销毁了,怎么可以
$ b $通过B类的析构函数再次调用b''''的析构函数?

是否意味着即使在''a'的析构函数被B类'

construtor调用之后,它实际上并没有被销毁?


这个实验的动机是知道当你想要什么时候做什么
从任何类的构造函数中抛出异常并且你想要

在抛出异常之前正确销毁其所有成员对象。

调用成员的析构函数就像我上面做的那样以正确的方式实现

这个?


提前致谢,

Gurry

Suppose there''s a class A. There''s another class called B which looks
like this:

class B {
private:
A a;
public :
B() { a.~A() }
}

Notice that B has a member ''a'' which is an instance of class A. Also
notice that the default constructor of B calls the destructor of ''a''.

Let''s now use class B in a main() block:

void main() {
B obj;
}

My question is: when this main() is run, what happens?

Well, I myself have compiled and run this code and I found that the
destructor of ''a'' is called twice: first when ''obj'' is constructed
(because class B''s constructor calls ''a''s destructor explicitly) and
second time when the control exits the main() block and ''obj'' is
destroyed by calling class B''s destructor.

Now if ''a'' has already been destroyed in class B''s constructor, how can
''a''''s destructor be called again through class B''s destructor? Does
that mean that even after ''a''s destructor is called in class B''s
construtor, it isn''t actually destroyed?

My motive for this experiment is to know what to do when you want to
throw exceptions from a constructor of any class and you want to
properly destroy all its member objects before the exception is thrown.
Is calling members'' destructors like I did above a right way to achieve
this?

Thanks in advance,
Gurry

推荐答案

好吧,如果A类的析构函数对释放对象没有任何作用,然后

它只是调用任何其他方法。

因此,如果A的析构函数为空,那么它不会''做任何事情,当B级被摧毁时,a就被摧毁了。


我猜:)

Well, if destructor of A class does nothing with freeing objects, then
it''s just calling any other method.
So if destructor of A is empty, then it doesn''t do anything, and a is
destroyed when class B is destroyed.

I guess :)




gurry写道:

gurry wrote:
假设有一个A类。还有另一个叫做B的班级,看起来像这样:

B班{
私人:
A a;
公开:
B(){a。~A()}


请注意,B有一个成员''a''是一个A类的实例。另外
注意B的默认构造函数调用''a''的析构函数。

现在让我们在main()块中使用B类:

void main(){
B obj;
}
我的问题是:当这个main()运行时,会发生什么?
Suppose there''s a class A. There''s another class called B which looks
like this:

class B {
private:
A a;
public :
B() { a.~A() }
}

Notice that B has a member ''a'' which is an instance of class A. Also
notice that the default constructor of B calls the destructor of ''a''.

Let''s now use class B in a main() block:

void main() {
B obj;
}

My question is: when this main() is run, what happens?




我发生以下情况:

B :: B()

A :: A()

A ::〜()//你的显式调用

B ::〜B()//自动调用,因为obj的范围结束了

A ::〜A()//由生成的B类析构函数调用

当构造函数抛出异常时,你应该小心

解除分配资源(例如你在抛出异常之前分配了

的构建时间,因为没有析构函数会调用类的b $ b(因为对象被认为是

仅在构造函数完成后才构造。)

Catalin



I happens the following:
B::B()
A::A()
A::~() // your explicit call
B::~B() // called automatically, since the scope of obj ended
A::~A() // called by the generated destructor of class B

When an exception is thrown from the constructor, you should take care
of deallocating the resources (e.g. memory) you allocated at
construction time, before throwing the exception, because no destructor
of the class will be called (because the object is considered as
constructed only after the complete execution of the constructor).

Catalin




gurry写道:

gurry wrote:
假设有一个A类。还有另一个名为B的类看起来像这样:

B类{
私人:
A a;
公开:
B(){a。~A()}
}

请注意B有一个成员''a'',它是A类的一个实例。另外
注意B的默认构造函数调用''a''的析构函数。

让''现在在main()块中使用B类:

void main(){
B obj;
}

我的问题是:这个main()运行了,会发生什么?
Suppose there''s a class A. There''s another class called B which looks
like this:

class B {
private:
A a;
public :
B() { a.~A() }
}

Notice that B has a member ''a'' which is an instance of class A. Also
notice that the default constructor of B calls the destructor of ''a''.

Let''s now use class B in a main() block:

void main() {
B obj;
}

My question is: when this main() is run, what happens?




编译器barfs在空虚上,我想。它是int main(){}

之后,当B ::〜B()被调用时,程序将具有未定义的行为。析构函数总是会破坏每个成员,这里不可能是b
。那时候可能发生任何事情。


HTH,

Michiel Salters



The compiler barfs on void, I guess. It''s int main() { }
After that, the program will have Undefined Behavior when B::~B() is
called. The destructor always destroys every member, which isn''t
possible here. Anything may happen at that point.

HTH,
Michiel Salters


这篇关于构造函数内的成员析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆