|
本帖最后由 imjacob 于 2019-10-15 19:28 编辑
最近研究c语言的面向对象写法, 看到这个代码 c语言的面向对象写法还不错。
完整的代码在上面连接, 但很奇怪 以下代码竟然可以编译且正常运行,不解。
我抽取出来重要的内容如下:
typedef struct Shape_ops Shape_ops;
struct Shape_ops {
uintptr_t this_sz; /* beginwith 'this_sz' */
const char* (*name)(Shape*);
int (*sides)(Shape*);
void (*dtor)(Shape*);
};
struct square_color_ops {
Shape_ops; //还能这么写??
const char *(*color)(struct square_color*);
};
static struct square_color_ops _square_color_ops = {
.this_sz = sizeof(struct square_color),//还能这么写??
.color = square_color__color_v,
.dtor = square_color__dtor_v,//还能这么写??
};
为啥_square_color_ops 中 this_sz和 dtor 明明是 Shape_ops的成员,怎么可以这么写?
还有 square_color_ops 中 Shape_ops 直接写类型 也可以? |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|