#include <iostream>
typedef void(*ISSFUNC)(void*);
struct AInst
{
void* data;
ISSFUNC func;
};
AInst* mInst;
void saveAInst(void* inst, ISSFUNC func)
{
mInst = new AInst();
mInst->data = inst;
mInst->func = func;
}
void issueInst()
{
mInst->func(mInst->data);
delete mInst;
}
class AHandler
{
public:
virtual void print() = 0;
};
class A
{
private:
AHandler* mHandler;
static void funct(void* data)
{
A* a = reinterpret_cast<A*>(data);
a->mHandler->print();
}
public:
A(AHandler* handler = nullptr) : mHandler(handler)
{
saveAInst(this, &funct);
}
};
class AHandlerImpl1 : public AHandler
{
public:
virtual void print() override
{
std::cout << "Implementation 1\n";
}
};
class AHandlerImpl2 : public AHandler
{
public:
virtual void print() override
{
std::cout << "Implementation 2\n";
}
};
int main()
{
AHandlerImpl1 impl1;
AHandlerImpl2 impl2;
A a1(&impl1);
issueInst();
A a2(&impl2);
issueInst();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdHlwZWRlZiB2b2lkKCpJU1NGVU5DKSh2b2lkKik7CgpzdHJ1Y3QgQUluc3QKewogICAgdm9pZCogZGF0YTsKICAgIElTU0ZVTkMgZnVuYzsKfTsKCkFJbnN0KiBtSW5zdDsKCnZvaWQgc2F2ZUFJbnN0KHZvaWQqIGluc3QsIElTU0ZVTkMgZnVuYykKewogICAgbUluc3QgPSBuZXcgQUluc3QoKTsKICAgIG1JbnN0LT5kYXRhID0gaW5zdDsKICAgIG1JbnN0LT5mdW5jID0gZnVuYzsKfQoKdm9pZCBpc3N1ZUluc3QoKQp7CiAgICBtSW5zdC0+ZnVuYyhtSW5zdC0+ZGF0YSk7CiAgICBkZWxldGUgbUluc3Q7Cn0KCmNsYXNzIEFIYW5kbGVyCnsKcHVibGljOgogICAgdmlydHVhbCB2b2lkIHByaW50KCkgPSAwOwp9OwoKY2xhc3MgQQp7CnByaXZhdGU6CiAgICBBSGFuZGxlciogbUhhbmRsZXI7CgogICAgc3RhdGljIHZvaWQgZnVuY3Qodm9pZCogZGF0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