Agn*_*ian 45 c oop inheritance
是否可以使用C建模继承?怎么样?示例代码将有所帮助.
编辑:我希望继承数据和方法.仅靠集装箱船无济于事.可替代性 - 使用基类对象工作的任何派生类对象 - 是我所需要的.
qrd*_*rdl 31
这样很简单:
struct parent {
int foo;
char *bar;
};
struct child {
struct parent base;
int bar;
};
struct child derived;
derived.bar = 1;
derived.base.foo = 2;
Run Code Online (Sandbox Code Playgroud)
但是如果你使用MS扩展(在GCC使用-fms-extensions标志中)你可以使用匿名嵌套structs,它会看起来好多了:
struct child {
struct parent; // anonymous nested struct
int bar;
};
struct child derived;
derived.bar = 1;
derived.foo = 2; // now it is flat
Run Code Online (Sandbox Code Playgroud)
Jér*_*nig 13
你绝对可以用(某种程度上)面向对象的方式编写C语言.
封装可以通过将结构的定义保存在.c文件中而不是在关联的头中来完成.然后外部世界通过保持指向它们来处理您的对象,并且您提供接受诸如对象的"方法"之类的指针的函数.
类似多态的行为可以通过使用函数指针来获得,函数指针通常分组在"操作结构"中,类似于C++对象中的"虚方法表"(或者它所称的任何东西).ops结构还可以包括其他内容,例如常量,其值特定于给定的"子类"."父"结构可以通过通用void*指针保持对ops特定数据的引用.当然,"子类"可以重复多个继承级别的模式.
因此,在下面的示例中,struct printer类似于抽象类,可以通过填充pr_ops结构"派生" ,并提供构造函数包装pr_create().每个子类型都有自己的结构,struct printer通过data通用指针"锚定"到对象.这是由fileprinter子类型进行的.人们可以想象一个基于GUI或基于套接字的打印机,无论代码的其余部分如何作为struct printer *参考都可以进行操作.
Printer.h中:
struct pr_ops {
void (*printline)(void *data, const char *line);
void (*cleanup)(void *data);
};
struct printer *pr_create(const char *name, const struct output_ops *ops, void *data);
void pr_printline(struct printer *pr, const char *line);
void pr_delete(struct printer *pr);
Run Code Online (Sandbox Code Playgroud)
printer.c:
#include "printer.h"
...
struct printer {
char *name;
struct pr_ops *ops;
void *data;
}
/* constructor */
struct printer *pr_create(const char *name, const struct output_ops *ops, void *data)
{
struct printer *p = malloc(sizeof *p);
p->name = strdup(name);
p->ops = ops;
p->data = data;
}
void pr_printline(struct printer *p, const char *line)
{
char *l = malloc(strlen(line) + strlen(p->name) + 3;
sprintf(l, "%s: %s", p->name, line);
p->ops->printline(p->data, l);
}
void pr_delete(struct printer *p)
{
p->ops->cleanup(p->data);
free(p);
}
Run Code Online (Sandbox Code Playgroud)
最后,fileprinter.c:
struct fileprinter {
FILE *f;
int doflush;
};
static void filepr_printline(void *data, const char *line)
{
struct fileprinter *fp = data;
fprintf(fp->f, "%s\n", line);
if(fp->doflush) fflush(fp->f);
}
struct printer *filepr_create(const char *name, FILE *f, int doflush)
{
static const struct ops = {
filepr_printline,
free,
};
struct *fp = malloc(sizeof *fp);
fp->f = f;
fp->doflush = doflush;
return pr_create(name, &ops, fp);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
是的,您可以使用“类型校正”技术来模拟继承性C。那是派生类内部基类(结构)的声明,并将派生类强制转换为基类:
struct base_class {
int x;
};
struct derived_class {
struct base_class base;
int y;
}
struct derived_class2 {
struct base_class base;
int z;
}
void test() {
struct derived_class d;
struct derived_class2 d2;
d.base.x = 10;
d.y = 20;
printf("x=%i, y=%i\n", d.base.x, d.y);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果要在程序中将派生转换为基类,则必须在派生结构的第一个位置声明基类:
struct base *b1, *b2;
b1 = (struct base *)d;
b2 = (struct base *)d2;
b1->x=10;
b2->x=20;
printf("b1 x=%i, b2 x=%i\n", b1->x, b2->x);
Run Code Online (Sandbox Code Playgroud)
在此代码段中,您只能使用基类。
我在项目中使用了此技术:oop4c