[clug] Object-Oriented C?

Paul Wayper paul.wayper at anu.edu.au
Wed Feb 22 02:17:06 GMT 2006


Hi all!

I wanted a set of C functions that could display and control a simple
'progress bar' on the (text) screen.  Because I couldn't see one already
written, and because I've already implemented one in Perl, I wrote it
again in C.  It's pretty simple but works nicely, and I'm still adding
features to it.  Interested people can find it at
http://biojanus.anu.edu.au/~paulway/progress.c and
http://biojanus.anu.edu.au/~paulway/progress.h, or by doing svn checkout
svn://tangram.dnsalias.net/pwlib-c/trunk (with a bunch of my other
'handy' routines).

Anyway, I've heard several people talk about writing C in an
object-oriented way.  I've done the simplest thing here, which is that
each function takes a PInfo pointer as its first argument, which is the
'object' that it operates on.  This isn't ver object-oriented at all, in
that you have to constantly tell the 'method' which object to orient
itself toward.  The more complex way would be to have a structure a bit
like this:

typedef struct object_struct {
    int public_property;
    int (*method)(int value);
} Object;

Thus if you had "Object *foo" you could call foo->method(42).  When you
get a new object the 'constructor' would put the pointers to the actual
methods into the Object struct so that the call was correct.  The
problem is, of course, that the actual method function enters and sees -
what?  No context, no object, no nothing.  Implicit in the 'constructor'
of object-oriented actions is the idea that the method call gets told
_which_ object it's operating on - Perl does it semi-explicitly by
assuming that the first parameter is a reference to the object.

So how do I do this in C?  Is it even possible?  The only method I can
think of involves some sort of trickery with knowing the address of the
method function pointer and subtracting a known offset for each method
that takes you back to the start of the actual object structure - in the
same way that talloc puts its own information before the start of any
pointer allocated by talloc.  But this still seems like opening the box
with the crowbar inside.  Do I have to stick to the
'pseudo-object-oriented' approach?

Have fun,

Paul

P.S.  I'd also be interested in GPLing this code, and sharing it in the
grand tradition of Open Source Software.  There must be dozens of little
implementations like this sitting around in people's source folders
because they've never really thought it was worth sharing components
like these.  Maybe I need to pore through the entire results of Googling
"C text progress bar", but it took me shorter than that to write the
damn thing... :-)  And besides, as Tridge says, who needs an excuse to
reimplement something?  IMNSHO how to share our code and (perhaps more
importantly) how (and where) to find others' shared code is a good topic
for an upcoming Programmers SIG...

-- 
-- Paul Wayper at ANU - +61 2 6125 0643
-- Research Programmer and System Administrator



More information about the linux mailing list