华科复试面试题:C语言如何实现C++ 中的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <stdio.h>
typedef struct Person{ char name; int age; void (*EatFunction)(struct Person this, int num); }Person;
void EatFunction(struct Person this, int num){ printf("Test\n"); }
Person *NewPerson(Person *this){ this->name = 'A'; this->age = 18; this->EatFunction = EatFunction; }
int main(){ Person person; NewPerson(&person); person.EatFunction(person,0); return 0; }
|
注意:测试的时候要保存为.c
格式,.cpp
格式运行会报错,因为 C++中 this
是关键字 。
参考博客:http://dongxicheng.org/cpp/ooc/