From 7af291e549a427b5942e5cf4b5be76426fad628f Mon Sep 17 00:00:00 2001 From: jf-home Date: Sun, 1 Sep 2024 18:40:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=BC=E4=B9=8E=E4=B8=8D=E8=83=BD=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E8=AF=A5=E5=8F=82=E6=95=B0=EF=BC=8C=E5=9B=A0=E4=B8=BA?= =?UTF-8?q?=E4=B8=8D=E5=80=9F=E5=8A=A9=E5=AD=90=E7=B1=BB=E7=9A=84=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BD=93=E4=BF=A1=E6=81=AF=EF=BC=8C=E6=98=AF=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E9=80=9A=E8=BF=87=E7=88=B6=E7=B1=BB=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=88=B0=E5=AD=90=E7=B1=BB=E7=BB=93=E6=9E=84=E4=BD=93=E8=B5=B7?= =?UTF-8?q?=E5=A7=8B=E7=82=B9=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/common.h | 1 - library/rect.c | 3 ++- library/shape.c | 13 ++++++++++--- src/main.c | 6 ++++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/library/common.h b/library/common.h index 152bf32..a734e10 100644 --- a/library/common.h +++ b/library/common.h @@ -7,7 +7,6 @@ #include #include #include -#include // [warring] offsetof is redefined in stddef.h // diff --git a/library/rect.c b/library/rect.c index b6790e7..5b896d3 100644 --- a/library/rect.c +++ b/library/rect.c @@ -4,7 +4,8 @@ double rect_area(void *self) { double area = 0; - struct _rect *pthis = container_of(self, struct _rect, me); + struct _rect *pthis = *(struct _rect **)self; + // struct _rect *pthis = container_of(self, struct _rect, me); area = pthis->shape->vtb.area(pthis); return area; } diff --git a/library/shape.c b/library/shape.c index 7cbc702..c21a95f 100644 --- a/library/shape.c +++ b/library/shape.c @@ -23,13 +23,20 @@ struct _virtual_table *virtual_new(void) return vtb; } - +#include "rect.h" double shape_area(void *self) { double area = 0; - struct _shape *pthis = container_of(self, struct _shape, me); - area = pthis->vtb.area(pthis); + struct _shape *parent = (*(struct _rect **)self)->shape; + area = parent->vtb.area(self); + + // struct _shape *parent = *(struct _shape **)self; + // void * pthis = container_of(self, struct _rect, shape); + // area = parent->vtb.area(pthis); + + // struct _rect *pthis = (struct _rect *)parent; + // pthis->shape->vtb.draw(self, NULL); return area; } diff --git a/src/main.c b/src/main.c index 413f5b9..011e59d 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { struct _rect *rect = rect_new(4, 5); - rect_area(rect); + rect_area(&rect); rect_draw(rect); struct _circle *circle = circle_new(2); @@ -15,7 +15,9 @@ int main(int argc, char *argv[]) circle_draw(circle); printf("----- rect & circle -----\n"); - shape_area(rect->shape); + // shape_area(rect); + + shape_area(&rect); shape_draw(rect->shape); shape_area(circle->shape);