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 < the malloc .h> . 4 // define a parent-child class virtual function . 5 static int Demo_virtual_add (Demo pThis *, int value); . 6 static int Derived_virtual_add (pThis the Derived *, int value); . 7 // 2. Determining the type of virtual function table, the virtual function table structure is used to create the virtual function table, the virtual memory address of the function 8 // object is to achieve polymorphic add function . 9 struct the vtbl { 10 int (* Padd) (the Derived * , int); // 3. Function pointer definition . 11 }; 12 is struct classdemo { 13 is struct the vtbl the vptr *; // . 1. Defined virtual function table pointer, the pointer type 14 int mi The; 15 int MJ; 16 }; . 17 static struct the vtbl g_Demo_vtbl = { 18 is Demo_virtual_add . 19 }; // . 4. Vtable global variables, static variables the keyword hidden in this file, can not be accessed outside 20 is static struct the vtbl g_Derived_vtbl = { 21 is Derived_virtual_add 22 is }; 23 is struct{classderived 24 struct classdemo D; 25 int MK; 26 is }; 27 Demo Demo_create * ( int I, int J) { 28 struct classdemo RET * = ( struct classdemo *) the malloc ( the sizeof ( struct classdem O)); // allocate space 29 IF (RET =! NULL) { 30 RET-> = the vptr & g_Demo_vtbl; // . 5. The correlation and the object-specific virtual function table up 31 is RET-> = mi The I; 32 RET-> MJ = J; 33 is } 34 is return RET; 35 } 36 int Demo_geti (Demo pThis *) { // embodied function casts 37 [ struct classdemo * obj = ( struct classdemo * ) pThis; 38 is return obj- > mi The; 39 } 40 int Demo_getj (Demo * pThis) { 41 is struct classdemo * obj = ( struct classdemo * ) pThis; 42 is return obj-> MJ; 43 is } 44 is //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 voidDemo_free (Demo * pThis) { 54 is Free (pThis); 55 } 56 is the Derived * Derived_create ( int I, int J, int K) { 57 is struct classderived * RET = ( struct classderived *) the malloc ( the sizeof ( struct Cl assderived)) ; 58 IF (! RET = NULL) { 59 RET-> d.vptr = & g_Derived_vtbl; // linked to the virtual function table subclass up 60 RET-> d.mi = I; 61 is 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 }