What will be printed to stdout after the following code execution?
#include<iostream>

struct A {
        ~A() {
                std::cout << "A";
        }
};

struct B :  public A {
        B() {
                throw 1;
        }

        ~B() {
                std::cout << "B";
        }
};


int main() {
        try {
                B b;
        }
        catch(...) { }
}
Explanation
When an exception is thrown in the constructor and is not caught in it, the call to the destructor of this class is not happening. Therefore, "B" will not be printed. But in the same situation, in accordance with the standard, 15.3 - 11 destructors of fully constructed base classes and subobjects will be called. At the moment, when the constructor B is entered, the object of the parent class A will already be constructed. Therefore, "A" will be displayed.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты