多态测试通过

This commit is contained in:
建峰 2024-09-01 16:54:38 +08:00
parent fd35e92c42
commit c1f45b94a4
3 changed files with 36 additions and 45 deletions

View File

@ -1,36 +0,0 @@
# cmake_demo
#### Description
{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
#### Software Architecture
Software architecture description
#### Installation
1. xxxx
2. xxxx
3. xxxx
#### Instructions
1. xxxx
2. xxxx
3. xxxx
#### Contribution
1. Fork the repository
2. Create Feat_xxx branch
3. Commit your code
4. Create Pull Request
#### Gitee Feature
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
4. The most valuable open source project [GVP](https://gitee.com/gvp)
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)

View File

@ -12,4 +12,4 @@ C语言实现思路
| --- | --- | --- | --- | | --- | --- | --- | --- |
| 匿名结构体 | 指针 | 多重继承,成员可能冲突 | B->A, C->A, D->B,D->C| | 匿名结构体 | 指针 | 多重继承,成员可能冲突 | B->A, C->A, D->B,D->C|
| 虚函数表 | 指针 | 子类父类必须将虚表放在结构体最开头,且不支持多继承下的多态 | | | 虚函数表 | 指针 | 子类父类必须将虚表放在结构体最开头,且不支持多继承下的多态 | |
| 虚函数多参数 | 指针 | 继承的父类无顺序强制要求,且可以多继承。但类方法参数变多 | | | 虚函数多参数 | 指针 | 继承的父类无顺序强制要求,且可以多继承。但类方法参数变多。多继承需要人为注意是调用了其他类还是继承了其他类 | |

43
main.c
View File

@ -120,7 +120,8 @@ double rect_area(void *self, void *parent)
void rect_draw(void *self, void *parent) void rect_draw(void *self, void *parent)
{ {
printf("rect draw\n"); struct _rect *pthis = (struct _rect *)parent;
pthis->shape->vtb.draw(self, NULL);
} }
double _rect_area(void *self, void *parent) double _rect_area(void *self, void *parent)
@ -181,13 +182,27 @@ struct _circle
// void (*draw)(void *self); // void (*draw)(void *self);
}; };
double circle_area(struct _circle *self, void *parent) double circle_area(void *self, void *parent)
{ {
double area = 3.14 * self->radius * self->radius; double area = 0;
struct _circle *pthis = (struct _circle *)parent;
area = pthis->shape->vtb.area(self, NULL);
return area;
}
void circle_draw(void *self, void *parent)
{
struct _circle *pthis = (struct _circle *)parent;
pthis->shape->vtb.draw(self, NULL);
}
double _circle_area(void *self, void *parent)
{
struct _circle *pthis = (struct _circle *)self;
double area = 3.14 * pthis->radius * pthis->radius;
printf("circle area = %0.2f\n", area); printf("circle area = %0.2f\n", area);
return area; return area;
} }
void circle_draw(struct _circle *self, void *parent) void _circle_draw(void *self, void *parent)
{ {
printf("circle draw\n"); printf("circle draw\n");
} }
@ -196,8 +211,8 @@ struct _circle *circle_new(double radius)
{ {
// super // super
static struct _virtual_table vtb = { static struct _virtual_table vtb = {
.area = (double (*)(void *self, void *parent))circle_area, .area = (double (*)(void *self, void *parent))_circle_area,
.draw = (void (*)(void *self, void *parent))circle_draw, .draw = (void (*)(void *self, void *parent))_circle_draw,
}; };
struct _shape *shape = shape_new(); struct _shape *shape = shape_new();
@ -231,8 +246,14 @@ struct _copper_cash
double copper_cash_area(struct _copper_cash *self, void *parent) double copper_cash_area(struct _copper_cash *self, void *parent)
{ {
double circle_area = self->circle->shape->vtb.area(self->circle, self->circle); // 如果用的继承,那么就不能直接调用父类的虚接口,实现自己。
double rect_area = self->rect->shape->vtb.area(self->rect, self->rect); // 因为父类虚接口,是自己实现的。这样相当于自己调用自己。导致卡死。
// double circle_area = self->circle->shape->vtb.area(self->circle, self->circle);
// double rect_area = self->rect->shape->vtb.area(self->rect, self->rect);
double circle_area = 3.14 * self->circle->radius * self->circle->radius;
double rect_area = self->rect->width * self->rect->height;
double area = circle_area - rect_area; double area = circle_area - rect_area;
printf("copper cash area = %0.2f\n", circle_area - rect_area); printf("copper cash area = %0.2f\n", circle_area - rect_area);
@ -265,12 +286,14 @@ struct _copper_cash *copper_cash_new(double width, double height, double radius)
{ {
return NULL; return NULL;
} }
rect->shape = shape;
struct _circle *circle = circle_new(radius); struct _circle *circle = circle_new(radius);
if (circle == NULL) if (circle == NULL)
{ {
return NULL; return NULL;
} }
circle->shape = shape;
// self // self
struct _copper_cash *cc = calloc(1, sizeof(struct _copper_cash)); struct _copper_cash *cc = calloc(1, sizeof(struct _copper_cash));
@ -324,6 +347,10 @@ int main(int argc, char *argv[])
printf("----- [2] copper_cash -----\n"); printf("----- [2] copper_cash -----\n");
rect_area(cc, cc->rect); rect_area(cc, cc->rect);
rect_draw(cc, cc->rect);
circle_area(cc, cc->circle);
circle_draw(cc, cc->circle);
return 0; return 0;
} }