#include <iostream>
#include <string>
#include <memory>
using namespace std;
struct A
{
int x;
std::string y;
A(int x, std::string y) : x(x), y(y) {}
A(A&& a) : x(std::move(a.x)), y(std::move(a.y)) {}
virtual const char* who() const { return "A"; }
void show() const { std::cout << (void const*)this << " " << who() << " " << x << " [" << y << "]" << std::endl; }
};
struct B : A
{
virtual const char* who() const { return "B"; }
B(A&& a) : A(std::move(a)) {}
};
template<class TO_T>
inline TO_T* turn_A_to(A* a) {
A temp(std::move(*a));
a->~A();
return new(a) B(std::move(temp));
}
int main()
{
A* pa = new A(123, "text");
pa->show(); // 0xbfbefa58 A 123 [text]
turn_A_to<B>(pa);
pa->show(); // 0xbfbefa58 B 123 [text]
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bWVtb3J5PgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBBCnsKICAgIGludCB4OwogICAgc3RkOjpzdHJpbmcgeTsKICAgIEEoaW50IHgsIHN0ZDo6c3RyaW5nIHkpIDogeCh4KSwgeSh5KSB7fQogICAgQShBJiYgYSkgOiB4KHN0ZDo6bW92ZShhLngpKSwgeShzdGQ6Om1vdmUoYS55KSkge30KCiAgICB2aXJ0dWFsIGNvbnN0IGNoYXIqIHdobygpIGNvbnN0IHsgcmV0dXJuICJBIjsgfQogICAgdm9pZCBzaG93KCkgY29uc3QgeyBzdGQ6OmNvdXQgPDwgKHZvaWQgY29uc3QqKXRoaXMgPDwgIiAiIDw8IHdobygpIDw8ICIgIiA8PCB4IDw8ICIgWyIgPDwgeSA8PCAiXSIgPDwgc3RkOjplbmRsOyB9Cn07CgpzdHJ1Y3QgQiA6IEEKewogICAgdmlydHVhbCBjb25zdCBjaGFyKiB3aG8oKSBjb25zdCB7IHJldHVybiAiQiI7IH0KICAgIEIoQSYmIGEpIDogQShzdGQ6Om1vdmUoYSkpIHt9Cn07Cgp0ZW1wbGF0ZTxjbGFzcyBUT19UPiA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