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
| struct Object { const struct Class * class; };
struct Class { const char * name; const struct Class * super; size_t size; void * (* ctor) (void * self, va_list * app); void * (* dtor) (void * self); int (* differ) (const void * self, const void * b); int (* puto) (const void * self, FILE * fp); };
struct Class Object = {.name = "Object"};
struct Point { const struct Object _; int x, y; };
struct PointClass { const struct Class _; void (* draw) (const void * self); } Point = {_.name = "Point", _.super = &Object, _.size = sizeof(struct Point), _.ctor = Point_ctor, .draw = Point_draw};
p = new(Point, 1, 2);
|
Object-oriented design patterns in the kernel 1,多态部分
Object-oriented design patterns in the kernel 2,继承部分
下载保存
https://www.cs.princeton.edu/courses/archive/spring21/cos217/
https://www.cs.princeton.edu/courses/archive/spring21/cos217/lectures/
1 封装
- 参考文件操作 FILE*
- 使用不完全类型
- 头文件分成 2 个,一个对外接口,一个用来给子类嵌套继承
2 多态
两种实现方式,常用第 2 种,文章对内核种第 2 种使用的特殊情况做了讨论
1. 最简单的方法是结构体包含函数指针,函数第一个参数为结构体指针。bar->foo ...