实验:面向对象的C 2 [英] Experiment: Object Oriented C?

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

问题描述

可能重复:结果
  你会写面向对象code用C <? / A>

Possible Duplicate:
Can you write object oriented code in C?

你好!

只是为了好玩,我一直在这最后两天的实验与创建纯C.我一直在用宏,动态链接,类型描述结构和玩弄围绕一个非常简单,非常直接的对象环境之类的,我在下面的到来:

Just for the fun of it, I've been experimenting these last two days with creating a very simple, very straightforward object environment in pure C. I've been toying around with macros, dynamic linking, type-description structures and the like, and I've arrived at the following:

string_o str = new(String, "hello world");
list_o list = new(List);

List.pushf(list, str);

printf("In the list: \"%s\"\n",
       String.text(List.popf(list)));

delete(list);
delete(str);

外观和工作方式还挺不错的,但我不明白的方式伪造实例方法。我不能让过去 Class.function(实例),不是没有为函数名,这违背了封装的目的,全球宏观的替代品。

Looks and works kinda nice, but I can't figure a way to fake instance methods. I can't get past Class.function(instance), not without global macro replacements for function names, which defeats the purpose of encapsulation.

再次,这是一个实验,只是为挑战和乐趣=)。你们能不能帮我想出一个办法做到这一点?我不想使用额外的preprocessing,只是普通的C和GCC宏。

Again, this is an experiment, just for the challenge and the fun =). Can you guys help me figure out a way to do this? I don't want to use additional preprocessing, only plain C and GCC macros.

编辑>忘了说 - 我不希望每个实例包含在其结构中的函数指针。这将使我的方法的语法还好,但那就意味着4个字节的数据对象有十几函数指针复制到每个实例。这有点像欺骗= P哈哈

edit> forgot to say -- I don't want each instance to contain the function pointers in its structure. That would give me method syntax alright, but it would mean that a 4-byte data object would have a dozen function pointers copied over to each instance. That's kinda like cheating =P haha

在此先感谢!

推荐答案

在C目标方向通常与函数指针完成的。这意味着它包含不仅对一个实例中的数据,但功能以及调用的结构。

Object orientation in C is normally done with function pointers. That means a structure which contains not only the data for an instance but the functions to call as well.

这是做继承和多态性C.举例来说最简单的方法,这里是一个orientd对象的通信例子。

It's the easiest way to do inheritance and polymorphism in C. By way of example, here's an object-orientd communications example.

它只有一个方法打开,但你可以看到不同的对TCP和HTML的子类。由具有初始化程序这台类特定的功能,你得到的多态性。

It only has one method open but you can see how that differs for the TCP and HTML sub-classes. By having an initialisation routine which sets the a class-specific function, you get polymorphism.

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

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

一个更完整的答案可以发现<一个href=\"http://stackoverflow.com/questions/351733/can-you-write-object-oriented-$c$c-in-c/351745#351745\">here.

A more complete answer can be found here.

在回应您的评论:

我不希望包含在每一个实例中的功能。

I don't want the functions contained in every single instance.

您也许是对的。这是不必要的重复时,这将是一个类的每个实例相同的信息。

You're probably right. It's unnecessary to duplicate that information when it will be the same for every instance of a single class.

有围绕一个简单的方法。而不是每个实例携带自己的一套函数指针,您可以创建一个结构持有它们的类,则每个实例获得一个指向结构。

There's a simple way around that. Rather than having every instance carry its own set of function pointers, you create one structure holding them for the class, then each instance gets a pointer to that structure.

这将节省相当多的空间,不必做间接两个层面来调用一个函数的(最小的)成本。

That will save quite a bit of space at the (minimal) cost of having to do two levels of indirection to call a function.

这篇关于实验:面向对象的C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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