There are situations that exception could be throw within constructor and we usually employ auto_ptr to protect resources. But if we do not have auto_ptr can we use the following technique to handle it?
Please search "QUESTION" and see my question.
Thank you for helping!!!
#include <cstdio>
using namespace std;
class E1 {
public: E1() {
int i = 1;
int j = 2;
int k = 3;
printf("i = %d, j = %d, k = %d\n", i, j, k);
// printf("E1(): throw()\n");
// throw 1;
}
public: ~E1() {
printf("~E1()\n");
}
};
class E2 {
public: E2() {
int i = 1;
int j = 2;
int k = 3;
printf("i = %d, j = %d, k = %d\n", i, j, k);
printf("E2(): throw()\n");
throw 1;
}
public: ~E2() {
printf("~E2()\n");
}
};
class D {
private: E1* e1;
private: E2* e2;
private: int marker;
public: D() : marker(0), e1(0), e2(0){
try {
printf("D()\n");
e1 = new E1();
marker = 1;
e2 = new E2();
// QUESTION:
// Is it possible that marker = 2 be executed before E2()
// throws in modern out-of-order processors?
marker = 2;
} catch(...) {
printf("marker = %d\n", marker);
}
if (marker == 0) {
}
else if (marker == 1) {
delete e1;
} else if (marker == 2) {
delete e1;
delete e2;
}
}
public: ~D() {
printf("~D()\n");
}
};
int main() {
D d;
return 0;
}