如何能继承用C来建模? [英] How can Inheritance be modelled using C?

查看:117
本文介绍了如何能继承用C来建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用C模型继承?怎么样?示例code会有所帮助。

Is it possible to model inheritance using C? How? Sample code will help.

编辑:我期待继承数据和方法。集装箱本身并不能帮助。替代 - 使用任何派生类对象,其中一个基类对象的作品 - 是我需要的。

I am looking to inherit both data and methods. Containership alone will not help. Substitutability - using any derived class object where a base class object works - is what I need.

推荐答案

您绝对可以在(有点)面向对象的风格写C。

You can definitely write C in a (somewhat) object-oriented style.

封装可以通过保持你的结构的定义来完成
在.c文件而不是相关的头。
然后,外部世界通过保持指向他们处理你的对象,
你提供的函数接受这样的指针的方法
你的对象。

Encapsulation can be done by keeping the definitions of your structures in the .c file rather than in the associated header. Then the outer world handles your objects by keeping pointers to them, and you provide functions accepting such pointers as the "methods" of your objects.

多态性样行为可以通过使用函数指针来获得,
通常业务结构中的分组,
有点像在C ++对象的虚方法表
(或任何它被称为)。
该OPS结构还可以包括其他的东西,如
常数,其值是特定于给定子。
父结构能够保持OPS特定数据的参考
通过一个普通的无效* 指针。
当然,子可以重复多层次的格局
继承。

Polymorphism-like behavior can be obtained by using functions pointers, usually grouped within "operations structures", kind of like the "virtual method table" in your C++ objects (or whatever it's called). The ops structure can also include other things such as constants whose value is specific to a given "subclass". The "parent" structure can keep a reference to ops-specific data through a generic void* pointer. Of course the "subclass" could repeat the pattern for multiple levels of inheritance.

因此​​,在下面的例子中,结构打印机类似于一个抽象类,
它通过填写 pr_ops 结构可导出,
并提供一个构造函数包装 pr_create()
各亚型都会有自己的结构,这将是挂靠
结构打印机通过数据对象通用指针。
这是由 fileprinter 亚型demontrated。
人们可以想像一个GUI或基于Socket的打印机,
这将不考虑由code的其余部分被操纵
作为结构打印机* 引用。

So, in the example below, struct printer is akin to an abstract class, which can be "derived" by filling out a pr_ops structure, and providing a constructor function wrapping pr_create(). Each subtype will have its own structure which will be "anchored" to the struct printer object through the data generic pointer. This is demontrated by the fileprinter subtype. One could imagine a GUI or socket-based printer, that would be manipulated regardless by the rest of the code as a struct printer * reference.

Printer.h中:

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:

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:

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

这篇关于如何能继承用C来建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆