跳出块:do destructors被调用? [英] Goto out of a block: do destructors get called?

查看:123
本文介绍了跳出块:do destructors被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

  void foo()
{
{
CSomeClass吧;

//这里有一些代码...

goto label;

//和here ...
}

标签:
//这里...
}

是否会调用bar的析构函数?

解决方案

C ++标准说:


退出范围(12.4)是
调用所有构造的对象
自动存储持续时间
(3.7.2)(命名对象或临时)
在该范围中声明,在
它们的
声明的相反顺序。


因此答案是是。 $ b

Consider the following code:

void foo()
{
    {
        CSomeClass bar;

        // Some code here...

        goto label;

        // and here...
    }

label:
    // and here...
}

Will the destructor of bar be called ?

解决方案

The C++ Standard says:

On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration.

So the answer is "yes".

这篇关于跳出块:do destructors被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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