其他分享
首页 > 其他分享> > 多态的C语言实现版本

多态的C语言实现版本

作者:互联网

  1 #ifndef _51_2_H                                                             
  2 #define _51_2_H
  3 typedef void Demo;
  4 typedef void Derived;
  5 Demo* Demo_create(int i,int j);
  6 int Demo_geti(Demo*pthis);
  7 int Demo_getj(Demo*pthis);
  8 int Demo_add(Demo*pthis,int value);
  9 void Demo_free(Demo*pthis);
 10 
 11 Derived*Derived_create(int i,int j,int k);
 12 int Derived_getk(Derived*pthis);
 13 int Derived_add(Derived*pthis,int value);
 14 #endif
  1 #include<stdio.h>                                                           
  2 #include"51-2.h"
  3 #include<malloc.h>
  4 //定义父子类的虚函数
  5 static int Demo_virtual_add(Demo*pthis,int value);
  6 static int Derived_virtual_add(Derived*pthis,int value);
  7 //2。确定虚函数表的类型,虚函数表的结构体,用来创建虚函数表,存储虚函数地址
  8 //目的是为了实现add函数的多态
  9 struct vtbl{
 10     int(*padd)(Derived*,int);//3。函数指针的定义
 11 };
 12 struct classdemo{
 13     struct vtbl* vptr;//1。定义虚函数表指针,指针的类型
 14     int mi;
 15     int mj;
 16 };
 17 static struct vtbl g_Demo_vtbl = {
 18     Demo_virtual_add
 19 };//4。全局虚函数表变量,static关键字将该变量隐藏在此文件中,外部不能访问
 20 static struct vtbl g_Derived_vtbl = {
 21     Derived_virtual_add
 22 };
 23 struct classderived{
 24     struct classdemo d;
 25     int mk;                                                                 
 26 };
 27 Demo* Demo_create(int i,int j){
 28     struct classdemo* ret = (struct classdemo*)malloc(sizeof(struct classdem    o));//分配空间
 29     if(ret != NULL){
 30         ret->vptr = &g_Demo_vtbl;//5。将虚函数表和具体对象关联起来
 31         ret->mi = i;
 32         ret->mj = j;
 33     }
 34     return ret;
 35 }
 36 int Demo_geti(Demo*pthis){//具体实现函数  强制类型转换
 37     struct classdemo* obj = (struct classdemo*)pthis;
 38     return obj->mi;
 39 }
 40 int Demo_getj(Demo*pthis){
 41     struct classdemo* obj = (struct classdemo*)pthis;
 42     return obj->mj;
 43 }
 44 //6。分析、实现具体的虚函数
 45 static int Demo_virtual_add(Demo*pthis,int value){                          
 46     struct classdemo* obj = (struct classdemo*)pthis;
 47     return obj->vptr->padd(pthis,value);
 48 }
 49 int Demo_add(Demo*pthis,int value){
 50     struct classdemo* obj = (struct classdemo*)pthis;
 51     return obj->mi + obj->mj + value;
 52 }
 53 void Demo_free(Demo*pthis){
 54     free(pthis);
 55 }
 56 Derived*Derived_create(int i,int j,int k){
 57     struct classderived* ret = (struct classderived*)malloc(sizeof(struct cl    assderived));
 58     if(ret != NULL){
 59         ret->d.vptr = &g_Derived_vtbl;//关联到子类的虚函数表上去
 60         ret->d.mi = i;
 61         ret->d.mj = j;
 60         ret->d.mi = i;
 61         ret->d.mj = j;
 62         ret->mk = k;
 63     }
 64     return ret;
 65 }
 66 int derived_getk(Derived* pthis){                                           
 67     struct classderived* obj = (struct classderived*)pthis;
 68     return obj->mk;
 69 }
 70 static int Derived_virtual_add(Derived*pthis,int value){
 71     struct classderived* obj = (struct classderived*)pthis;
 72     return obj->mk + value;
 73 }
 74 int Derived_add(Derived*pthis,int value){
 75     struct classderived* obj = (struct classderived*)pthis;
 76     return obj->d.vptr-> padd(pthis,value);
 77 }
  1 #include<stdio.h>                                                           
  2 #include"51-2.h"
  3 void run(Demo*p,int v){
  4     int r = Demo_add(p,v);
  5     printf("r=%d\n",r);
  6 }
  7 int main(){
  8     Demo* pb = Demo_create(1,2);
  9     Demo* pd = Derived_create(1,22,333);
 10 
 11     printf("Demo_add(3)=%d\n",Demo_add(pb,3));
 12     printf("Derived_add(3)=%d\n",Derived_add(pd,3));
 13 
 14     run(pb,3);
 15     run(pd,3);
 16     Demo_free(pb);
 17     Demo_free(pd);
 18     return 0;
 19 }

 

标签:struct,int,Demo,Derived,多态,C语言,add,版本,pthis
来源: https://www.cnblogs.com/DXGG-Bond/p/11967897.html