为什么在异常期间不调用这些析构函数?

时间:2019-02-13 17:34:08

标签: c++ exception destructor

根据输出,~Foo仅针对对象c

被调用
#include <stdexcept>
#include <iostream>

struct Foo
{
    const char * m_name;

    Foo (const char * name) : m_name (name) {}

    ~ Foo ()
    {
        std::cerr << m_name << bool(std::current_exception ()) << std::endl;
    }
};

int main ()
{
    Foo a ("a");
    {
        Foo b ("b");
        {
            Foo c ("c");
        }
        throw 0;
    }
}

输出:

c0
terminate called after throwing an instance of 'int'

为什么其他对象没有被破坏?我以为这应该发生。

0 个答案:

没有答案
相关问题