Fog Creek Software
Discussion Board




OOP in C

The FILE structure and its  functions fopen, fclose, fread, fwrite, fprintf, etc. is a good example of OOP in C. How about inheritance, polymorphism in C?

John
Friday, April 25, 2003

C++ was originally implemented as a preprocessor to C (C Front).

Object oriented style programming is possible using C.  Use C structs to hold the data.  Create functions to act on the structs that take a pointer to a struct instance as their first parameter.  "Mangle" the function names by prepending the name of the type of the struct that the function operates on.  Use malloc instead of new.  Voila--the poor man's C++ compiler.

The not-angry coder
Friday, April 25, 2003

Inheritance and polymorphism in C are frequently implemented through tables of function pointers - i.e. manually create vtables for your structs.

punter
Friday, April 25, 2003

http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html

cypher
Friday, April 25, 2003

Inheritance may be emulated by placing a struct inside another one:

struct Base { /* ... *};

struct Derived { struct Base super; /* ... */ }

Then, you may pass a Derived pointer to each method which requires a Base pointer.

Giovanni Corriga
Friday, April 25, 2003

Yes, please don't violate the language with bad casting:

struct fooBase {
int field1;
int field2;
};

struct fooInherit {
struct fooBase base;
int field3;
int field4;
};

struct fooInherit x;
struct fooBase *y;

y = (fooBase*)&x;    // DON'T DO IT!

y = &x.y;  // OK


I think I said that right.

Nat Ersoz
Friday, April 25, 2003

Everything mentioned so far will work, but you're still missing out on encapsulation.  I can't think of any way to get the language to help you out on that - I think you have to rely on programmer discipline (maybe aided by naming conventions).
As someone else mentioned, virtual functions can be done, but it could get hairy.
It's all possible to do in theory, it's just a matter of how much you need and how much work you're willing to do.  At some point, it becomes desirable to use a language that has more support for the features C doesn't support.

Brian
Friday, April 25, 2003

OOP can be implemented in any language that supports the call of functions by reference. OOP is not really supported though, unless the language makes it easy to do these sorts of things.

Several years ago there was an article in C Users Journal with a set of macros to do OO programming in C that you could use to compile either C or C++ code against. Pretty useless today, but a neat idea back in the day.

Chris Tavares
Friday, April 25, 2003

It's a good idea to actually define what OOP is or isn't for you -- it's ill defined, and sparks many religious wars that are about definition and not about merit.

A good summary can be found in Paul Graham's site, [ http://paulgraham.com/reesoo.html ].

He-who-would-not-be-named
Saturday, April 26, 2003

Wanna see OOP, polymorphism, inheritance and garbage collection done in C? download ghostscript source code.

anon
Saturday, April 26, 2003

*  Recent Topics

*  Fog Creek Home