In the following code, what is the order in which the destructors of b
, q
and e
are called, and which is called before handling the exception.
(The "cout..." parts are leftover for the original question)
#include <iostream>
using namespace std;
class A {
public:
A(int arg) : m(arg) {
cout << "A::A(int) " << m << endl;
m = 2*arg;
}
virtual void f() {
cout << "A::f() " << m << endl;
}
void g() {
cout << "A::g(A) " << m << endl;
}
int m;
};
class B : public A {
public:
B(int arg) : A(arg) {
cout << "B::B(int) " << m << endl;
m = 3*arg;
}
~B() {
cout << "B::~B()" << endl;
}
void f() {
cout << "B::f(A&) " << m << endl;
}
virtual void g() {
B q(*this);
throw q;
cout << "B::g(A) " << m << endl;
}
};
int main() {
try {
B b(1);
b.g();
} catch (A e) {
cout << "Error: ";
e.f();
}
return 0;
}
If it's possible, could you explain the reason. Thank you.