- 论坛徽章:
- 0
|
inet_addr 发表于 2012-05-09 16:22 ![]()
c语言中的“对象”是这样的?????
typedef struct {
应该是这样的:- struct girl_friend
- {
- void (*go_shopping)();
- void (*watch_movie)();
- };
- void buy_nothing() {}
- void buy_everything() {}
- void violent_movies() {}
- void love_movies() {}
- girl_friend *new_girl_friend(void (*shopping_style)(), void (*movie_type)())
- {
- girl_friend *gf = (girl_friend*)malloc(sizeof(girl_friend));
- gf->go_shopping = shopping_style;
- gf->watch_movie = movie_type;
- return gf;
- }
- void destroy_girl_friend(girl_friend *gf)
- {
- free(gf);
- }
- int main()
- {
- girl_friend *gf = new_girl_friend(&buy_everything, &violent_movies);
- gf->go_shopping();
- gf->watch_movie();
- destroy_girl_friend(gf);
- return 0;
- }
复制代码 |
|