标准ANSI C中的面向对象编程 [英] Object-oriented programming in standard ANSI C

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

问题描述




我对用于以面向对象方式编程的技术感兴趣

使用C ANSI语言。我正在研究GObject库和Laurent

Deniau在他的网站上发布的OOPC框架
http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html 。这种方法很有启发性。

非常有启发性。我知道我可以使用例如

C ++来完成这些工作,但使用

纯ANSI C实现这些概念的智力挑战与我相关。


您是否了解其他方法?有没有在生产代码中使用这种

技术的经验?在GNOME世界中使用GObject似乎很好

,但我对Laurent没有太多了解

Deniau的OOPC。您对这些技术的优缺点有什么评论吗?


非常感谢您的宝贵帮助和意见


最好的问候


Thierry

Hi,

I''m interested in techniques used to program in an object-oriented way
using the C ANSI language. I''m studying the GObject library and Laurent
Deniau''s OOPC framework published on his web site at
http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is
very instructive. I know that I could do much of this stuff with e.g.
C++, but the intellectual challenge of implementing these concepts with
pure ANSI C is relevant to me.

Are you aware of another approaches? Any experience in using such
techniques in production code? The use of GObject seems to be well
implemented in the GNOME world, but I didn''t find much about Laurent
Deniau''s OOPC. Have you some comments about the strengths and drawbacks
of such techniques?

Many thanks for your valuable help and comments

Best regards

Thierry

推荐答案



11月15日下午6:28,Thierry Chappuis < thie ... @ mujigka.chwrote:


On Nov 15, 6:28 pm, "Thierry Chappuis" <thie...@mujigka.chwrote:




我对以前的技术感兴趣以面向对象的方式编程

使用C ANSI语言。我正在研究GObject库和Laurent

Deniau在他的网站上发布的OOPC框架:http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html 。这种方法很有启发性。

非常有启发性。我知道我可以使用例如

C ++来完成这些工作,但使用

纯ANSI C实现这些概念的智力挑战与我相关。


您是否了解其他方法?有没有在生产代码中使用这种

技术的经验?在GNOME世界中使用GObject似乎很好

,但我对Laurent没有太多了解

Deniau的OOPC。您对这些技术的优缺点有什么评论吗?


非常感谢您的宝贵帮助和意见


最好的问候


Thierry
Hi,

I''m interested in techniques used to program in an object-oriented way
using the C ANSI language. I''m studying the GObject library and Laurent
Deniau''s OOPC framework published on his web site athttp://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is
very instructive. I know that I could do much of this stuff with e.g.
C++, but the intellectual challenge of implementing these concepts with
pure ANSI C is relevant to me.

Are you aware of another approaches? Any experience in using such
techniques in production code? The use of GObject seems to be well
implemented in the GNOME world, but I didn''t find much about Laurent
Deniau''s OOPC. Have you some comments about the strengths and drawbacks
of such techniques?

Many thanks for your valuable help and comments

Best regards

Thierry



是的。这是一个有趣的话题。实际上,C不能像C ++那样完全支持
OOP,但是我们可以使用一些技巧来实现* * *

面向对象的编程语言。例如,我们可以使用

结构来替换类,将函数指针用作

结构的成员,就像类的方法一样。事实上,在Linux内核源代码中,我们已经应用了这样的OO创意。

Yeah. This is an interesting topic. In fact, C can NOT fully support
OOP like C++, but we can use some tricks to make it *like* an
object-oriented programming language. For example, we can use
structures to replace classes, use function pointer as a member of a
structure like a method of a class. And in fact, in Linux kernel source
code, there are really such OO ideas applied.


11月15日,10日:上午15点,丛王 < xiyou.wangc ... @ gmail.comwrote:
On Nov 15, 10:15 am, "Cong Wang" <xiyou.wangc...@gmail.comwrote:

ThierryYeah。这是一个有趣的话题。事实上,C不能完全支持
ThierryYeah. This is an interesting topic. In fact, C can NOT fully support



OOP就像C ++一样,但是我们可以使用一些技巧使它像*一样

对象面向编程语言。例如,我们可以使用

结构来替换类,将函数指针用作

结构的成员,就像类的方法一样。事实上,在Linux内核源代码中,b $ b代码实际上已经应用了这样的OO思想。

OOP like C++, but we can use some tricks to make it *like* an
object-oriented programming language. For example, we can use
structures to replace classes, use function pointer as a member of a
structure like a method of a class. And in fact, in Linux kernel source
code, there are really such OO ideas applied.



这只是OO编程的三个要求之一

语言,封装。另外两个是继承和

多态,并且在C中仿效更难。我已经通过利用C将允许的事实看到了b $ b看到的继承你

将任何类型转换为任何其他类型而不抱怨。所以基本上:


struct父母

{

int a,b;

};


struct孩子

{

int a,b;

更多变量

};


struct Child2

{

int a,b;

more变量

}


如果你想将一个Child或一个Child2传递给一个函数,而不是

制作两个版本的该函数(需要不同的名称因为

在C中没有重载),你可以让函数取一个Parent,

然后传递一个Child或一个Child2' 'c''说(父母)c。我不确定这是不是很有效,或者如果确实在

C标准中定义了结构的变量出现在内存中

命令它们在声明中指定。


我没有看到任何方法做多态。我猜这有点类似于C ++中的模板,虽然模板在技术上并不是多态,但是它们是实现多态性影响的黑客。这是因为

模板在编译时处理,真正的OO多态性是
运行时多态性。


Colin K.

That''s just one of the three requirements for an OO programming
language, encapsulation. The other two are inheritance and
polymorphism, and those are much more difficult to emulate in C. I''ve
seen inheritance attempted by exploiting the fact that C will let you
cast any type to any other type without complaining. So basically:

struct Parent
{
int a,b;
};

struct Child
{
int a,b;
more variables
};

struct Child2
{
int a,b;
more variables
}

If you want to pass a Child or a Child2 to a function, instead of
making two version of the function (which need different names because
there is no overloading in C), you can make the function take a Parent,
and then pass a Child or a Child2 ''c'' by saying (Parent)c. I''m not
sure if this just happens to work, or if it is actually defined in the
C standard that the variables of a structure appear in memory in the
order that they are specified in the declaration.

I haven''t seen any way to do polymorphism. I guess that''s kind of like
templates in C++, although templates are technically not polymorphism,
they are a hack to acheive the effect of polymorphism. This is because
the templates are handled at compile time, and true OO polymorphism is
runtime polymorphism.

Colin K.


raza ... @ gmail.com写道:
raza...@gmail.com wrote:

11月15日,10日:上午15点,丛王 < xiyou.wangc ... @ gmail.comwrote:
On Nov 15, 10:15 am, "Cong Wang" <xiyou.wangc...@gmail.comwrote:

ThierryYeah。这是一个有趣的话题。事实上,C不能完全支持
ThierryYeah. This is an interesting topic. In fact, C can NOT fully support



OOP就像C ++一样,但是我们可以使用一些技巧使它像*一样

对象面向编程语言。例如,我们可以使用

结构来替换类,将函数指针用作

结构的成员,就像类的方法一样。事实上,在Linux内核源代码中,b $ b代码实际上已经应用了这样的OO思想。

OOP like C++, but we can use some tricks to make it *like* an
object-oriented programming language. For example, we can use
structures to replace classes, use function pointer as a member of a
structure like a method of a class. And in fact, in Linux kernel source
code, there are really such OO ideas applied.



这只是OO编程的三个要求之一

语言,封装。另外两个是继承和

多态,并且在C中仿效更难。我已经通过利用C将允许的事实看到了b $ b看到的继承你

将任何类型转换为任何其他类型而不抱怨。


That''s just one of the three requirements for an OO programming
language, encapsulation. The other two are inheritance and
polymorphism, and those are much more difficult to emulate in C. I''ve
seen inheritance attempted by exploiting the fact that C will let you
cast any type to any other type without complaining.



< snip>

<snip>


我还没有看到任何做多态的方法...
I haven''t seen any way to do polymorphism...



< snip>


但是使用真正的OO启用语言(如
C ++)的最大收获是对OO结构的编译器支持。对于大多数非嵌入式平台而言,C并不是为
OOP而设计的,而且随着C ++编译器的可用性越来越高,使用后者更为明智,(或者

另一种OO语言),除非有充分的理由支持C,(比如

编写低级系统代码,或者希望获得最大的可移植性等)。

<snip>

But the biggest gain of all in using a true OO enabled language like
C++ is the compiler support for OO constructs. C wasn''t designed for
OOP and with increasing availability of C++ compilers for most
non-embedded platforms, it would be wiser to use the latter, (or
another OO language), unless there''re strong reasons to favour C, (like
writing low-level system code, or desiring maximum portability etc).


这篇关于标准ANSI C中的面向对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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