1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #ifndef virtual
5 #define virtual
6 #endif
7
8 //===========================================
9 // Utility functions
10 //===========================================
11
12 typedef void (*Constructor_Func)(void*); // constructor function
13 typedef void (*Destructor_Func)(void*); // destructor function
14
15 //-------------------------------------------
16 // New()
17 // create a new object
18 // parameters:
19 // cfn : constructor function
20 // size: size of object to create
21 // return values:
22 // poniter of the object created
23 //-------------------------------------------
24 void* New(void* cfn,size_t size)
25 {
26 void* obj = malloc(size);
27 if(obj && cfn ){
28 ((Constructor_Func)cfn)(obj);
29 }
30 return obj;
31 }
32
33 //-------------------------------------------
34 // Delete()
35 // destory an object
36 // parameters:
37 // dfn : destructor function
38 // this: object to destory
39 // return values:
40 // no return values.
41 //-------------------------------------------
42 void Delete(void* dfn,void* this)
43 {
44 if(this&&dfn){
45 ((Destructor_Func)dfn)(this);
46 }
47 free(this);
48 }
49
50 //===============================================
51 // Classes
52 //===============================================
53
54 //-----------------------------------------------
55 // Animal class
56 //----------------------------------------------
57
58 //
59 typedef virtual void (*Output_Func)(void*); // virtual function
60 typedef struct __Animal
61 {
62 int __no_use;
63 void* vf_table[1]; // virtual function table
64 }Animal;
65 // constructor for Animal class
66 void Animal_ctor(Animal* this)
67 {
68 this->__no_use = 0;
69 this->vf_table[0] = 0; // initialzie virtual function table
70 }
71 // virtual function definition in Animal class
72 virtual void Animal_Output(Animal* this)
73 {
74 ((Output_Func)(this->vf_table[0]))(this);
75 }
76
77 //--------------------------------------------------
78 // Dog class
79 //--------------------------------------------------
80 typedef struct __Dog
81 {
82 Animal base;
83 }Dog;
84 // override virtual function definition in Dog class
85 virtual void Dog_Output(Dog* this)
86 {
87 printf("This is a dog!\n");
88 }
89 // constructor for Dog class
90 void Dog_ctor(Dog* this)
91 {
92 // call constructor of base class
93 Animal_ctor(&this->base);
94 // rewrite the virtual function table
95 this->base.vf_table[0] = Dog_Output;
96 }
97
98 //--------------------------------------------------
99 // Cat class
100 //--------------------------------------------------
101 typedef struct __Cat
102 {
103 Animal base;
104 }Cat;
105 // override virtual function definition in Cat class
106 virtual void Cat_Output(Cat* this)
107 {
108 printf("This is a cat!\n");
109 }
110 // constructor for Cat class
111 void Cat_ctor(Cat* this)
112 {
113 // call constructor of base class
114 Animal_ctor(&this->base);
115 // rewrite the virtual function table
116 this->base.vf_table[0] = Cat_Output;
117 }
118
119 //-----------------------------------------------
120 // main entry point
121 //-----------------------------------------------
122 int main(int argc, char* argv[])
123 {
124 Animal* pa1 = New(Dog_ctor,sizeof(Dog)); // this animal is a dog
125 Animal* pa2 = New(Cat_ctor,sizeof(Cat)); // this animal is a cat
126
127 Animal_Output(pa1); // call virtual function on dog
128 Animal_Output(pa2); // call virtual function on cat
129
130 Delete(0,pa1);
131 Delete(0,pa2);
132 getchar();
133 return 0;
134 }
135
2 #include <stdio.h>
3 #include <stdlib.h>
4 #ifndef virtual
5 #define virtual
6 #endif
7
8 //===========================================
9 // Utility functions
10 //===========================================
11
12 typedef void (*Constructor_Func)(void*); // constructor function
13 typedef void (*Destructor_Func)(void*); // destructor function
14
15 //-------------------------------------------
16 // New()
17 // create a new object
18 // parameters:
19 // cfn : constructor function
20 // size: size of object to create
21 // return values:
22 // poniter of the object created
23 //-------------------------------------------
24 void* New(void* cfn,size_t size)
25 {
26 void* obj = malloc(size);
27 if(obj && cfn ){
28 ((Constructor_Func)cfn)(obj);
29 }
30 return obj;
31 }
32
33 //-------------------------------------------
34 // Delete()
35 // destory an object
36 // parameters:
37 // dfn : destructor function
38 // this: object to destory
39 // return values:
40 // no return values.
41 //-------------------------------------------
42 void Delete(void* dfn,void* this)
43 {
44 if(this&&dfn){
45 ((Destructor_Func)dfn)(this);
46 }
47 free(this);
48 }
49
50 //===============================================
51 // Classes
52 //===============================================
53
54 //-----------------------------------------------
55 // Animal class
56 //----------------------------------------------
57
58 //
59 typedef virtual void (*Output_Func)(void*); // virtual function
60 typedef struct __Animal
61 {
62 int __no_use;
63 void* vf_table[1]; // virtual function table
64 }Animal;
65 // constructor for Animal class
66 void Animal_ctor(Animal* this)
67 {
68 this->__no_use = 0;
69 this->vf_table[0] = 0; // initialzie virtual function table
70 }
71 // virtual function definition in Animal class
72 virtual void Animal_Output(Animal* this)
73 {
74 ((Output_Func)(this->vf_table[0]))(this);
75 }
76
77 //--------------------------------------------------
78 // Dog class
79 //--------------------------------------------------
80 typedef struct __Dog
81 {
82 Animal base;
83 }Dog;
84 // override virtual function definition in Dog class
85 virtual void Dog_Output(Dog* this)
86 {
87 printf("This is a dog!\n");
88 }
89 // constructor for Dog class
90 void Dog_ctor(Dog* this)
91 {
92 // call constructor of base class
93 Animal_ctor(&this->base);
94 // rewrite the virtual function table
95 this->base.vf_table[0] = Dog_Output;
96 }
97
98 //--------------------------------------------------
99 // Cat class
100 //--------------------------------------------------
101 typedef struct __Cat
102 {
103 Animal base;
104 }Cat;
105 // override virtual function definition in Cat class
106 virtual void Cat_Output(Cat* this)
107 {
108 printf("This is a cat!\n");
109 }
110 // constructor for Cat class
111 void Cat_ctor(Cat* this)
112 {
113 // call constructor of base class
114 Animal_ctor(&this->base);
115 // rewrite the virtual function table
116 this->base.vf_table[0] = Cat_Output;
117 }
118
119 //-----------------------------------------------
120 // main entry point
121 //-----------------------------------------------
122 int main(int argc, char* argv[])
123 {
124 Animal* pa1 = New(Dog_ctor,sizeof(Dog)); // this animal is a dog
125 Animal* pa2 = New(Cat_ctor,sizeof(Cat)); // this animal is a cat
126
127 Animal_Output(pa1); // call virtual function on dog
128 Animal_Output(pa2); // call virtual function on cat
129
130 Delete(0,pa1);
131 Delete(0,pa2);
132 getchar();
133 return 0;
134 }
135
浙公网安备 33010602011771号