iDevGames Forums
OOP in C? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: OOP in C? (/thread-4681.html)



OOP in C? - Leisure Suit Lurie - Dec 23, 2005 11:40 AM

http://www.accu.org/acornsig/public/articles/oop_c.html

Mildly interesting.

I think ThemsallTook has/had a similar stance.


OOP in C? - akb825 - Dec 23, 2005 01:12 PM

I've practiced that several times. You also see it quite a bit with Carbon and somewhat with OpenGL. (with texture objects, display lists, etc., but the "object" you store is actually an int that tells it which one to use) It can certainly be very useful.


OOP in C? - ThemsAllTook - Dec 23, 2005 01:22 PM

Indeed, this is a great way to code. I'll admit I've been primarily using Objective-C nowadays, but I still like this approach a lot. It's too bad the article didn't get into subclassing... I would have liked to see if his approach was similar to mine.


OOP in C? - Leisure Suit Lurie - Dec 23, 2005 01:23 PM

I can't imagine "faking" inheritance in C, but you guys are the gurus...


OOP in C? - Corun - Dec 23, 2005 01:38 PM

Faking inheritance isn't hard... I'd do it like this, or rather, this is how I've done it/seen it done:

Code:
typedef struct
{
     int member1;
     float member2;
     char[] stringy;
} baseClass;

typedef struct
{
    baseClass superClass;
    int subclassMember1;
    float subclassMember2;
} subClass

Then, just use casting whenever you call functions that take the base class (when you have the superclass).


OOP in C? - zKing - Dec 23, 2005 01:39 PM

Yep, I've always been fond of using OO designs even when the implementation language is C. I think you can very easily implement information hiding and seperation of interface vs implementation in C.

As for inheritance, while I do use it often, I find it to be the most abused aspect of OO languages and I don't miss having an "easy" form of it if I'm in a non OO language.

A long time ago I worked with a woman who had an entire C macro system that completely implemented v-tables w/inheritance etc behind the scenes. It was pretty slick, but it was overkill and reduced the maintainability of the code... to each their own.


OOP in C? - Leisure Suit Lurie - Dec 23, 2005 02:07 PM

Who needs inheritance when we have copy and paste? Rasp

j/k


OOP in C? - Ingemar - Dec 27, 2005 06:20 AM

Leisure Suit Lurie Wrote:I can't imagine "faking" inheritance in C, but you guys are the gurus...
It isn't even faked, IMHO. The old Mac Toolbox (pre-Carbon) does it with GrafPtr/WindowPeek/DialogPeek. It is a clear case of subclassing. Replacing methods as well as inheriting them or calling the method of the superclass are nicely done with some ProcPtrs (like in SAT and TransSkel) Mac Toolbox doesn't do that most of the time though. The only drawback is that it gets somewhat clumsy to access fields in inherited classes (which is why Mac Toolbox had both WindowPtrs and WindowPeek).

The big win with doing OO this way is compatibility. Your code can easily be mixed with any other language, since it has standard C interfaces everywhere, and methods are C-style procedure pointers. Everything can cope with that. Built-in OOP extensions are generally incompatible with the rest of the world. (Standards are good, so everybody wants their own, you know.)


OOP in C? - Chris Burkhardt - Dec 31, 2005 12:58 PM

Yep, I think Ingemar's Sprite Animation Toolkit was the first time I saw object-oriented technique in a plain procedural language (It was Pascal, not C like this article, but close enough).