当委托构造函数抛出时,析构函数是否被调用? [英] Is the destructor called when a delegating constructor throws?

查看:121
本文介绍了当委托构造函数抛出时,析构函数是否被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,如果构造函数throws,那么所有完全构造的子对象将以相反的顺序被销毁,包括成员数据和所有类型的基类。析构函数不会为非委托构造函数调用。对于委托构造函数,当构造函数体被输入时,对象已经被构造,但构造继续一些。因此,问题出现的是类的析构函数是否被调用,如果委托构造函数在其体内抛出异常?

It is well known that if a constructor throws, then all fully constructed sub-objects will be destroyed in reverse order including member data and all kinds of base classes. The destructor does not get called for non-delegating constructors though. For a delegating constructor the object has been constructed when the constructor body is entered, but construction continues somewhat. Therefore the question arises whether the destructor of the class is called, if the delegating constructor throws an exception from within its body?

class X
{
public:
    X();
    X(int) : X() { throw std::exception(); } // is ~X() implicitely called?
    ~X();
};


推荐答案

规则是析构函数
构造对象。一旦任何构造函数完成,对象被认为是完全构造的
,包括委托的
构造函数(即使程序在另一个
构造函数中继续)。

The rule is that the destructor is called for all fully constructed objects. The object is considered fully constructed once any constructor has finished, including the delegated constructor (even though the program continues in another constructor).

这篇关于当委托构造函数抛出时,析构函数是否被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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