#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"A init"<<endl;
showptr();
cout<<"throwing..."<<endl;
throw 1;
}
~A()
{
cout<<"A deinit"<<endl;
showptr();
}
void showptr()
{
cout<<"A "<<this<<endl;
}
};
class B
{
public:
B() : ClassChain(NULL)
{
ClassChain = new A;
cout<<"B init"<<endl;
showptr();
}
~B()
{
cout<<"B deinit"<<endl;
showptr();
delete ClassChain;
ClassChain = NULL;
}
A* ClassChain;
void showptr()
{
cout<<"B "<<this<<endl;
}
};
int main()
{
B* myClass = NULL;
try
{
myClass = new B;
}
//guaranteed throw
catch(int)
{
delete myClass;
myClass = NULL;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIEEKewogICAgcHVibGljOgogICAgICAgIEEoKQogICAgICAgIHsKICAgICAgICAgICAgY291dDw8IkEgaW5pdCI8PGVuZGw7CiAgICAgICAgICAgIHNob3dwdHIoKTsKICAgICAgICAgICAgY291dDw8InRocm93aW5nLi4uIjw8ZW5kbDsKICAgICAgICAgICAgdGhyb3cgMTsKICAgICAgICB9CiAgICAgICAgfkEoKQogICAgICAgIHsKICAgICAgICAgICAgY291dDw8IkEgZGVpbml0Ijw8ZW5kbDsKICAgICAgICAgICAgc2hvd3B0cigpOwogICAgICAgIH0KCiAgICAgICAgdm9pZCBzaG93cHRyKCkKICAgICAgICB7CiAgICAgICAgICAgIGNvdXQ8PCJBICI8PHRoaXM8PGVuZGw7CiAgICAgICAgfQp9OwoKY2xhc3MgQgp7CiAgICBwdWJsaWM6CiAgICAgICAgQigpIDogQ2xhc3NDaGFpbihOVUxMKQogICAgICAgIHsKICAgICAgICAgICAgQ2xhc3NDaGFpbiA9IG5ldyBBOwogICAgICAgICAgICBjb3V0PDwiQiBpbml0Ijw8ZW5kbDsKICAgICAgICAgICAgc2hvd3B0cigpOwogICAgICAgIH0KICAgICAgICB+QigpCiAgICAgICAgewogICAgICAgICAgICBjb3V0PDwiQiBkZWluaXQiPDxlbmRsOwogICAgICAgICAgICBzaG93cHRyKCk7CiAgICAgICAgICAgIGRlbGV0ZSBDbGFzc0NoYWluOwogICAgICAgICAgICBDbGFzc0NoYWluID0gTlVMTDsKICAgICAgICB9CgogICAgICAgIEEqIENsYXNzQ2hhaW47CgogICAgICAgIHZvaWQgc2hvd3B0cigpCiAgICAgICAgewogICAgICAgICAgICBjb3V0PDwiQiAiPDx0aGlzPDxlbmRsOwogICAgICAgIH0KfTsKCmludCBtYWluKCkKewogICAgQiogbXlDbGFzcyA9IE5VTEw7CiAgICB0cnkKICAgIHsKICAgICAgICBteUNsYXNzID0gbmV3IEI7CiAgICB9CiAgICAvL2d1YXJhbnRlZWQgdGhyb3cKICAgIGNhdGNoKGludCkKICAgIHsKICAgICAgICBkZWxldGUgbXlDbGFzczsKICAgICAgICBteUNsYXNzID0gTlVMTDsKICAgIH0KICAgIHJldHVybiAwOwp9