开发者

Object oriented programming in C [duplicate]

This question already has answers here: Closed 12 years ago.

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 im开发者_如何学运维plemented in ANSI C:

  • virtual functions
  • inheritance
  • best practice

A book about OOP programming ANSI C would be great too.


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!


  1. Objective-C isn't C - it's a different language.
  2. C isn't an object-oriented language. It's possible to do some OO-style stuff, but it's not what it's built for.
  3. Best practices in C are procedural.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜