What will be printed to stdout after the following code execution?

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

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

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

int main() {
        try {
                B b;
        catch(...) { }
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
Зворотній Зв’язок
Зареєструйся Зараз
або Підпишись на майбутні тести