#include <iostream>
#include <functional>
using namespace std;
using EatPtr = function<void()>;
using PlayPtr = function<void()>;
struct VirtualTable {
EatPtr m_eat;
PlayPtr m_play;
};
struct Base {
VirtualTable m_table;
};
struct DeriveA {
Base m_base;
};
struct DeriveB {
Base m_base;
};
void BaseEat() {
cout << "base eat" << endl;
}
void BasePlay() {
cout << "base play" << endl;
}
void DeriveAEat() {
cout << "derive a eat" << endl;
}
void DeriveAPlay() {
cout << "derive a play" << endl;
}
void DeriveBEat() {
cout << "derive b eat" << endl;
}
void DeriveBPlay() {
cout << "derive b play" << endl;
}
int main() {
DeriveA a;
DeriveB b;
a.m_base.m_table.m_eat = DeriveAEat;
a.m_base.m_table.m_play = DeriveAPlay;
b.m_base.m_table.m_eat = DeriveBEat;
b.m_base.m_table.m_play = DeriveBPlay;
Base* basea = (Base*)&a;
basea->m_table.m_eat();
basea->m_table.m_play();
Base* baseb = (Base*)&b;
baseb->m_table.m_eat();
baseb->m_table.m_play();
return 0;
}