如何使用Cbuild立inheritance?

有可能使用Cbuild模inheritance吗? 怎么样? 示例代码将有所帮助。

编辑:我正在寻找inheritance数据和方法。 集装箱船只是无济于事。 可替代性 – 使用基类对象工作的任何派生类对象 – 是我所需要的。

走这样很简单:

struct parent { int foo; char *bar; }; struct child { struct parent base; int bar; }; struct child derived; derived.bar = 1; derived.base.foo = 2; 

但是,如果你使用MS扩展(在GCC中使用-fms-extensions标志),你可以使用匿名嵌套struct ,它会看起来好多了:

 struct child { struct parent; // anonymous nested struct int bar; }; struct child derived; derived.bar = 1; derived.foo = 2; // now it is flat 

你可以肯定地写一个(有些)面向对象的风格的C.

封装可以通过将结构的定义保存在.c文件而不是关联的头文件中来完成。 然后外部世界通过保持指针来处理你的对象,并且提供接受这些指针的函数作为你的对象的“方法”。

类似于多态的行为可以通过使用函数指针来获得,通常在“操作结构”中分组,类似于C ++对象中的“虚拟方法表”(或称之为任何东西)。 ops结构还可以包含其他的东西,如常量,其值是特定于“子类”的。 “父”结构可以通过通用的void*指针保持对特定于ops的数据的引用。 当然,“子类”可以重复多级inheritance的模式。

因此,在下面的例子中, struct printer类似于抽象类,可以通过填充pr_ops结构来“派生”,并提供一个构造函数wrap pr_create() 。 每个子types都将有自己的结构,通过data通用指针“绑定”到struct printer对象。 这是由fileprinter子typesfileprinter 。 人们可以想象一个基于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); 

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

最后,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); } 

至less在一定程度上应该是可能的。

你需要build模什么? 数据或方法的inheritance?

编辑 :这是我发现的一个简短的文章: http : //fluff.info/blog/arch/00000162.htm

由于早期版本的C ++主要是一个转换为C的预处理器,所以这是完全可能的。

我在C中使用了一个对象系统,它使用了后期绑定的方法,允许使用reflection进行面向对象的操作。

你可以在这里阅读。

这个链接可能是有用的 – > 链接

基本的例子就像下面

  struct BaseStruct { // some variable } struct DerivedStruct { struct BaseStruct lw; // some more variable }; 

是的,你可以使用“types双关”技术来模仿遗传。 这是派生类中基类(struct)的声明,并将派生作为基础进行强制转换:

 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; dy = 20; printf("x=%i, y=%i\n", d.base.x, dy); } 

但是,如果要在派生结构中强制派生基类,则必须在派生结构的第一个位置声明基类:

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

在这个片段中,你只能使用基类。

我在我的项目中使用这种技术: oop4c

 #include <stdio.h> ///////Class Cobj typedef struct Cobj{ int x; void (*setptr)(char * s,int val); int (*getptr)(char * s); } Cobj; void set(char * s,int val) { Cobj * y=(Cobj *)s; y->x=val; } int get(char * s){ Cobj * y=(Cobj *)s; return y->x; } ///////Class Cobj Cobj s={12,set,get}; Cobj x; void main(void){ x=s; x.setptr((char*)&x,5); s.setptr((char*)&s,8); printf("%d %d %d",x.getptr((char*)&x),s.getptr((char*)&s) ,sizeof(Cobj)); }