C 中的面向对象编程
Possible Duplicate:
Can you write object oriented code in C?
Hi, can someone point me to a tutorial explain me how OOP concepts can be implemented in ANSI C:
- virtual functions
- inheritance
- best practice
A book about OOP programming ANSI C would be great too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议查看 GTK+ GUI 工具包 的内部结构。它是一个用 C 语言编写的面向对象的系统,展示了您所追求的技术。它是开源的,因此没有什么可以阻止您阅读和学习。
I would recommend looking at the internals of the GTK+ GUI toolkit. It's an object-oriented system written in C, showing off the techniques you're after. It is open source, so there's nothing stopping you from reading it and learning.
面向对象的机制并未被定义为 C 语言的功能。您必须通过在 C 的过程性质之上添加自己的逻辑来模拟面向对象。
我不建议在 C 中应用 OOP 的每个方面。虽然封装相对容易实现,但实现继承确实很难看使用一种并非为此而设计的语言。
一个很好的教程: http://www.planetpdf.com/codecuts/pdfs/ ooc.pdf
Object-oriented mechanisms aren't defined as features of the C language. You'll have to emulate object-orientation by adding your own logic on top of the procedural nature of C.
I wouldn't recommend applying every aspect of OOP in C. While encapsulation is relatively easy to achieve, implementing inheritance would be really ugly in a language that wasn't made for that.
A good tutorial on that: http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
以下是有关此主题的书籍的链接: http://www.planetpdf.com /codecuts/pdfs/ooc.pdf(Ansi-C 中的面向对象编程 - 正是您所寻找的)。
祝你好运并有耐心。用 C 语言编写 OOP 并不是一件容易的事,但如果您喜欢极端的事情,可能会有所收获!
另一个起点是检查 GObject: http://library.gnome.org/devel/gobject/ 正如 @unwind 所说 GTK 是一个很好的例子在 C 中使用 OOP 概念。您还可以使用 GLib,这是一个很好的库,它将当然,它会让你的 C 编程生活变得更加轻松,它是以 OOP 方式编程的,而且还有一个优点是可移植!
Here is the link of a book on this topic: http://www.planetpdf.com/codecuts/pdfs/ooc.pdf (Object Oriented Programming in Ansi-C - exacty what you were looking for).
Good luck and have patience. Writing OOP in C is not an easy task, but can be somewhat rewarding if you like extreme things !
Another starting point will be to check GObject: http://library.gnome.org/devel/gobject/ and as @unwind said GTK is a good example of how to use OOP concepts in C. You can also take at GLib, is a nice library that will certainly make your C programming life a lot more easier, it is programmed in OOP manner, and as a plus is portable!