多态性用C [英] Polymorphism in C

查看:116
本文介绍了多态性用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计在C程序,操纵几何图形,这将是非常方便的,如果每一个类型的人物可以用相同的图元进行操作。

I'm designing a program in C that manipulates geometric figures and it would be very convenient if every type of figure could be manipulated by the same primitives.

我怎样才能做到这在C?

How can I do this in C?

推荐答案

您一般做它的函数指针。换句话说,即同时按住数据的的指针功能,这操作这些数据结构简单。我们做这种东西年前Bjarne的小号就到了现场。

You generally do it with function pointers. In other words, simple structures that hold both the data and pointers to functions which manipulate that data. We were doing that sort of stuff years before Bjarne S came onto the scene.

因此​​,例如,在通信类,你将有一个开放的,读,写,千钧一发这将保持在结构四个功能指针,旁边的数据为对象,​​是这样的:

So, for example, in a communications class, you would have an open, read, write and close call which would be maintained as four function pointers in the structure, alongside the data for an object, something like:

typedef struct {
    int (*open)(void *self, char *fspec);
    int (*close)(void *self);
    int (*read)(void *self, void *buff, size_t max_sz, size_t *p_act_sz);
    int (*write)(void *self, void *buff, size_t max_sz, size_t *p_act_sz);
    // And the data for the object goes here.
} tCommsClass;

tCommsClass commRs232;
commRs232.open = &rs232Open;
: :
commRs232.write = &rs232Write;

tCommsClass commTcp;
commTcp.open = &tcpOpen;
: :
commTcp.write = &tcpWrite;

这些函数指针的初始化实际上是在一个构造,如 rs232Init(tCommClass *),这将是负责建立的,默认状态具体的目的,以匹配特定的类

The initialisation of those function pointers would actually be in a "constructor" such as rs232Init(tCommClass*), which would be responsible for setting up the default state of that particular object to match a specific class.

当您从该类继承'',你只需要改变指针指向自己的函数。大家称这些功能会做到这一点通过函数指针,让您完全多态性:

When you 'inherit' from that class, you just change the pointers to point to your own functions. Everyone that called those functions would do it through the function pointers, giving you your polymorphism:

int stat = (commTcp.open)(commTcp, "bigiron.box.com:5000");

有点像一个手动配置的虚函数表的,在C ++的说法。

Sort of like a manually configured vtable, in C++ parlance.

您甚至可以通过设置指针为NULL -the行为将C ++因为稍有不同,你可能会得到在运行时核心转储,而不是在编译时错误有虚拟课堂。

You could even have virtual classes by setting the pointers to NULL -the behaviour would be slightly different to C++ inasmuch as you would probably get a core dump at run-time rather than an error at compile time.

下面是一块样品code的演示吧:

Here's a piece of sample code that demonstrates it:

#include <stdio.h>

// The top-level class.

typedef struct _tCommClass {
    int (*open)(struct _tCommClass *self, char *fspec);
} tCommClass;

// Function for the TCP class.

static int tcpOpen (tCommClass *tcp, char *fspec) {
    printf ("Opening TCP: %s\n", fspec);
    return 0;
}
static int tcpInit (tCommClass *tcp) {
    tcp->open = &tcpOpen;
    return 0;
}

// Function for the HTML class.

static int htmlOpen (tCommClass *html, char *fspec) {
    printf ("Opening HTML: %s\n", fspec);
    return 0;
}
static int htmlInit (tCommClass *html) {
    html->open = &htmlOpen;
    return 0;
}

&NBSP;

// Test program.

int main (void) {
    int status;
    tCommClass commTcp, commHtml;

    // Same base class but initialized to different sub-classes.
    tcpInit (&commTcp);
    htmlInit (&commHtml);

    // Called in exactly the same manner.

    status = (commTcp.open)(&commTcp, "bigiron.box.com:5000");
    status = (commHtml.open)(&commHtml, "http://www.microsoft.com");

    return 0;
}

这产生的输出:

Opening TCP: bigiron.box.com:5000
Opening HTML: http://www.microsoft.com

所以你可以看到,不同的函数被调用,根据不同的子类。

so you can see that the different functions are being called, depending on the sub-class.

这篇关于多态性用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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