mirror of
https://gitee.com/apaki/cmake_demo.git
synced 2025-07-03 15:56:53 +08:00
似乎不能取消该参数,因为不借助子类的结构体信息,是无法通过父类获取到子类结构体起始点的
This commit is contained in:
parent
37ea937147
commit
7af291e549
@ -7,7 +7,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
// [warring] offsetof is redefined in stddef.h
|
||||
//
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user