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.

Community
  • 1
  • 1
drahnr
  • 6,782
  • 5
  • 48
  • 75
  • 2
    Trying to hammer a square OO peg into a round C hole will result in rewriting C++. Just use C++ or another language. – Andy_Vulhop Sep 23 '10 at 13:52
  • I did not ask *is possible?* but *how to achieve*. And as GTK+, GGlib are done in ANSI C in OOP style, it probably has some right to exist. – drahnr Sep 23 '10 at 15:59

4 Answers4

7
  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
  • 69,215
  • 34
  • 177
  • 229
  • 1
    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. – drahnr Sep 23 '10 at 15:51
  • @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 Sep 23 '10 at 18:18
  • 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. – drahnr Sep 23 '10 at 21:09
  • @penguinpower: ok - no worries! – Skilldrick Sep 24 '10 at 07:51
7

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
  • 12,500
  • 24
  • 85
  • 118
  • 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. – drahnr Sep 23 '10 at 15:54
  • 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 Sep 23 '10 at 16:03
  • 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) – drahnr Sep 23 '10 at 16:07
  • I'd suggest you to try out [COOP](https://github.com/ShmuelFine/COOP) It features Classes, Inheritance, Exceptions, unit tests, and more. – Shmuel Fine Dec 21 '22 at 20:25
4

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
  • 391,730
  • 64
  • 469
  • 606
1

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
  • 42,498
  • 14
  • 94
  • 130