#include <stdio.h>
#include <string.h>
struct app
{
int x;
int y;
char name[10];
void (*init)(struct app *thisptr, int _x, int _y, const char *_name);
int (*calxy)(struct app *thisptr);
void (*end)(struct app *thisptr);
};
void init(struct app *thisptr, int _x, int _y, const char *_name)
{
thisptr->x = _x;
thisptr->y = _y;
strncpy(thisptr->name, _name, 10);
thisptr->name[9] = '\0';
}
int calxy(struct app *thisptr)
{
return thisptr->x * thisptr->y;
}
void end(struct app *thisptr)
{
printf("%s is end!\n", thisptr->name);
}
int main()
{
struct app apptest;
apptest.init = init;
apptest.calxy = calxy;
apptest.end = end;
apptest.init(&apptest, 10, 20, "apptest");
int ans = apptest.calxy(&apptest);
printf("ans:%d\n", ans);
apptest.end(&apptest);
return 0;
}
C语言面对对象的一种简单写法
最新推荐文章于 2023-10-19 20:00:01 发布