What will be printed as a result of the execution of the following program?
#include <iostream>
using namespace std;

struct A {
    A() { cout << "A"; }
    ~A() { cout << "~A"; }
    A(const A&) { cout << "=A"; }
};

int main()
{
    try
    {
        A a;
        throw a;
    }
    catch(A &e)
    {
    }
    return 0;
}
Explanation
The throw a; line will call the copy constructor to create a temporary copy of the a object, which will then be passed to the exception handler by reference. The a object created on the stack in the try block will be destroyed when exiting this block, so its destructor will be called. When exiting the catch block, the temporary copy of the object a will be destroyed, because it will no longer be referenced. That's why the ~A() destructor will be called at this moment.

Следи за CodeGalaxy

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

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