tags:

views:

193

answers:

4

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.

+6  A: 
  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.
Skilldrick
1. I know that, and the question was not about Objective-C nor did I even note it. 2. That's my point. And I know it was made forprocedural programming, but nobodysaid i onlywant to use it what it was designed for.3. see 2.4. If you don't answer myquestion at all, which I think I specified well enough to get the point: I want to know _how_ and not _is it sensible_, so eitherawser my question or just don't answer. This is Hijacking.
penguinpower
@penguinpower: The reason I said about Objective-C is that one of your tags was [objective-c]. I wasn't saying it wasn't sensible to do OO in C, it's just that it seemed from your question that you didn't realise the distinction. I didn't mean to hijack your question - I was trying to help with the basics.
Skilldrick
@Skilldrick: Must be a typo, started the thread from my phone which unfortunatly hast autocomplete functionalility for the german language :/ If you felt my comment was offense, it was not supposed to be offense.
penguinpower
@penguinpower: ok - no worries!
Skilldrick
+4  A: 

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!

Andrei Ciobanu
GTK+ is the UI I want to use and therefore I _want_ to learn how to properly use it. But unfortuanetly i 'd rather like to do it lecure by lecture than reverse engineering which takes a lot more time.
penguinpower
If you want to use GTK+ you are not only limited to C. There are bindings for other languages, so you can develop GTK applications in: C++, python, vala, C# and even Java.
Andrei Ciobanu
I know that too, but I want/need/prefer to reuse code of a existing GPLv2 project written in OOP style C, thats the point, and I am _not_ going to use extern or C++ hackz. In the longterm I see forward to the linux kernel (long long term)
penguinpower
+4  A: 

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.

unwind
GTK+ is the UI I want to use and therefore I want to learn how to properly use it. But unfortuanetly i 'd rather like to do it lecure by lecture than reverse engineering which takes a lot more time
penguinpower
A: 

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

Blagovest Buyukliev