29

简介

好的,所以在GTK+ 0.60 版本之后,设计人员意识到为了未来的发展和进步,需要将整个工具包重写为面向对象。

现在,由于 C 不支持 OOP,为了提供面向对象和继承层次结构,他们创建了GObject System。现在创建 GObject 系统肯定需要开发时间、更多的依赖项、更多的问题,但他们必须创建它来为C编程语言提供面向对象的能力。但当时,有另一种解决方案正好提供了C++

问题

为什么 GTK+ 的开发者不直接使用 C++?

说明

我的意思是,为什么要浪费时间创建整个库而不是使用许多项目采用的经过时间考验的解决方案?不要误会我的意思,我不是想把这篇文章变成 C 与 C++ 的东西(我在论坛上已经受够了,谢谢)。我只是想知道让 GTK+ 的设计者做出决定的原因和问题。

4

4 回答 4

25

I can't directly answer the question, at least as far as GTK goes. That answer lies with the GTK+ developers, so you'll have to hunt them down and ask them. But as for why one would want to add an object oriented system to C, instead of using C++, there are plenty of reasons. The three I would immediately think of are

  1. Language Complexity: While C is a pretty simple language, C++ is mind-numbingly complicated, with support for most (not all) C stuff, as well as conveniences like references, and also object-oriented features and a complex template language. And have you seen the new system of values: lvalues, rvalues, glvalues, prvalues and xvalues - huh? There's plenty more I could talk about. Given time, C++ becomes manageable, but it's still overkill for just wanting some object oriented features in C.

  2. Control: If the designers went with C++, they'd be stuck with C++ philosophy. For instance, multiple inheritance is a controversial idea, and for good reason. By design, the GObject system is built to only support single inheritance, which can drastically simplify the inheritance hierarchies. If the designers went with C++, there would be no way to limit users to a single inheritance system. Multiple inheritance is just an example - I'm sure there's plenty of other places in which the GObject system differs from the C++ ideology.

  3. Interoperability: This is probably a big one. Although there a few languages with which C++ interoperates cleanly, the fact is that C++ just isn't that great at interop. However, interoperability with C is almost taken for granted. C is often described as the lingua franca of programming languages, since it forms the de facto standard for interop. By designing a C API, the GObject designers opened the door to GTK+ development in any number of languages.

于 2012-03-17T05:49:37.620 回答
14

GObjects is intended to be language independent. It has dynamic typing, and you should it compare with a run-time system like COM, .NET or CORBA, rather than with specific languages. If you go into languages, then the features are more at the Objective-C than the C++ side.

于 2012-03-17T05:49:37.293 回答
13

The GObject type system does things that you can't do in C++. For one thing, it allows creation of new classes at runtime, and does this in a way that is language-independent - you can define a new class in Python at runtime, then manipulate instances of that class within a function written in C, which need not even be aware that Python was ever involved.

于 2012-03-17T05:51:05.550 回答
6

从问题中链接到的wiki :

历史(来自维基百科

GTK+ 最初是在 GNU Image Manipulation Program (GIMP) 中设计和使用的,作为 Motif 工具包的替代品;在某个时候,Peter Mattis 对 Motif 不再抱有幻想,并开始编写自己的 GUI 工具包,称为 GIMP 工具包,并成功地用 GIMP 的 0.60 版本取代了 Motif。[3] 最后,GTK 被重写为面向对象,并更名为 GTK+。这在 GIMP 的 0.99 版本中首次使用


这应该告诉您,这object-oriented paradigm不是选择 GTK 语言(与 GTK+ 不同)的最重要标准,并且该功能是在很久以后添加的。

于 2012-03-17T08:57:57.520 回答