C语言模拟实现C++多态

来源:互联网 发布:网络被骗多少钱立案 编辑:程序博客网 时间:2024/06/07 08:12
#include<stdio.h>#include<stdlib.h>typedef void(*fun)();//定义一个函数指针类型;struct A{fun f;int _a;};struct B{A s;int _b;};void fA(){printf("fA()\n");}void fB(){printf("fB()\n");}int main(){A a;B b;a.f = fA;b.s.f = fB;A* p = &a;p->f();//调用fA()p =(A*) &b;p->f();//调用fB()system("pause");return 0;}

0 0
原创粉丝点击