C语言实现c++多态
#include <iostream>
using namespace std;
typedef void (*EatPtr)();
typedef void (*PlayPtr)();
typedef struct _virtualPtrTable{
EatPtr eat;
PlayPtr play;
}VPtrTable;
typedef struct _base{
VPtrTable* vpointer;
}Base;
typedef struct _derive{
Base base;
int age;
}Derive;
void bEat(){
cout<<"base is eating"<<endl;
}
void dEat(){
cout<<"derive is eating"<<endl;
}
void bPlay(){
cout<<"base is play"<<endl;
}
void dPlay(){
cout<<"derive is play"<<endl;
}
VPtrTable BTable={bEat,bPlay};
VPtrTable DTable={dEat,dPlay};
int main(){
Base B;
Derive D;
B.vpointer=&BTable;
D.base.vpointer=&DTable;
Base* base;
base = &B;
base->vpointer->eat();
base->vpointer->play();
base = (Base*)&D;
base->vpointer->eat();
base->vpointer->play();
}