#include <iostream>
#include <iomanip>
using namespace std;
class Base
{
public:
Base() { ptr = new int[100]; cout << "alloc mem at " << ptr << endl; }
~Base() { delete [] ptr; cout << "free mem at " << ptr << endl; }
int * ptr;
};
class Derived1
{
public:
Derived1(int x, int y):x(x),y(y){}
Derived1(int x)
{
new(this) Derived1(x,0);
}
int x, y;
Base b;
};
class Derived2: public Base
{
public:
Derived2(int x, int y):x(x),y(y){}
Derived2(int x)
{
new(this) Derived2(x,0);
}
int x, y;
};
int main(int argc, const char * argv[])
{
Derived1 d1(5);
Derived2 d2(5);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBCYXNlCnsKcHVibGljOgogICAgQmFzZSgpICB7IHB0ciA9IG5ldyBpbnRbMTAwXTsgY291dCA8PCAiYWxsb2MgbWVtIGF0ICIgPDwgcHRyIDw8IGVuZGw7IH0KICAgIH5CYXNlKCkgeyBkZWxldGUgW10gcHRyOyBjb3V0IDw8ICJmcmVlIG1lbSBhdCAiIDw8IHB0ciA8PCBlbmRsOyB9CiAgICBpbnQgKiBwdHI7Cn07CgpjbGFzcyBEZXJpdmVkMQp7CnB1YmxpYzoKICAgIERlcml2ZWQxKGludCB4LCBpbnQgeSk6eCh4KSx5KHkpe30KICAgIERlcml2ZWQxKGludCB4KQogICAgewogICAgICAgIG5ldyh0aGlzKSBEZXJpdmVkMSh4LDApOwogICAgfQogICAgaW50IHgsIHk7CiAgICBCYXNlIGI7Cn07CgpjbGFzcyBEZXJpdmVkMjogcHVibGljIEJhc2UKewpwdWJsaWM6CiAgICBEZXJpdmVkMihpbnQgeCwgaW50IHkpOngoeCkseSh5KXt9CiAgICBEZXJpdmVkMihpbnQgeCkKICAgIHsKICAgICAgICBuZXcodGhpcykgRGVyaXZlZDIoeCwwKTsKICAgIH0KICAgIGludCB4LCB5Owp9OwoKCmludCBtYWluKGludCBhcmdjLCBjb25zdCBjaGFyICogYXJndltdKQp7CiAgICBEZXJpdmVkMSBkMSg1KTsKICAgIERlcml2ZWQyIGQyKDUpOwp9Cg==