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()
        A a;
        throw a;
    catch(A &e)
    return 0;
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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes