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);