typedef struct ab { int (*func)(int a,int b); }
http://dongxicheng.org/cpp/ooc/ 这个方法就是封装, 不过在linux内核中一般不用隐藏私有数据。 结构体中的函数指针可以实现c++式的,将数据和函数封装在一起 struct student { const struct stu_op *op; char name[100]; int score; }; struct stu_op { void (*set_name)(struct student * ); void (*set_score)(struct student *); } //下面两个结构体已经在别处定义好了。比如这两种方法可以是不同的实现方法、 等。比如op1是输入的方式, op2是从文件读进入。 extern const struct stu_op op1; extern const struct stu_op op2; struct student st1; st1->op = op1; ... //调用 st1.op->set_name(&st1); st1.op->set_score(&st1); 像这样的。。 这个方法多看看开源的库(用c实现的)一般都会使用。
70,020
社区成员
243,267
社区内容
加载中
试试用AI创作助手写篇文章吧