#include<stdio.h> void * ptr;/*为方便new的实现*/ #define virtual //void * virtual_tab[size]; typedef struct/*基类定义*/ { void * ptr; virtual void (*print)(); }Base; virtual void Base_print()/*基类虚函数实现*/ { printf("this is base/n"); } int Base_constructor(Base * base_ptr)/*基类构造函数*/ { base_ptr->print = Base_print; return 0; } /*-------------------------------------------------------*/ typedef struct { Base b;/*派生类定义*/ /*...*/ }Derive; virtual void Derive_print() { printf("this is derive/n"); } int Derive_constructor(Derive * derive_ptr)/*派生类构造函数*/ { *((void **)((void *)derive_ptr + 4)) = Derive_print; return 0; } #define new(type) (ptr = (type *)malloc(sizeof(type)))&&!type##_constructor(ptr)?ptr:0 /*模仿C++的new,申请内存的同时调用构造函数*/ int main() { Base * p,* q; p = new(Base); q = new(Derive); if(p == 0||q == 0)return 1; p->print(); q->print(); return 0; }