OOP in C?

Moderator
Posts: 529
Joined: 2003.03
Post: #1
http://www.accu.org/acornsig/public/articles/oop_c.html

Mildly interesting.

I think ThemsallTook has/had a similar stance.

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 529
Joined: 2003.03
Post: #4
I can't imagine "faking" inheritance in C, but you guys are the gurus...

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Quote this message in a reply
Member
Posts: 72
Joined: 2004.06
Post: #5
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).

"So long and thanks for all the fish" - In memory of Douglas Adams.
My Site
Quote this message in a reply
zKing
Unregistered
 
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 529
Joined: 2003.03
Post: #7
Who needs inheritance when we have copy and paste? Rasp

j/k

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #8
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.)
Quote this message in a reply
Member
Posts: 75
Joined: 2002.04
Post: #9
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).

Chris Burkhardt
Quote this message in a reply
Post Reply