Coding C in an OO fashion.

Started by
16 comments, last by Lucidquiet 18 years ago
I've heard its possible, but I don't know the specifics. You can accomplish data hiding (Encapsulation) fairly easilty by with C, but how would someone go about Polymorphism, and Inheritance, with C. (I'm working a on a new language, that is a cross between Python and C). Thanks for any help, L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Advertisement
This is generally a bad idea. It's possible, but extremely limited and messy. The GTK GUI toolkit does this, and it's more or less a nightmare. Regardless, GTK is an example (actually, I think that might be part of GLib), and I know I've read an article about this in Dr. Dobbs Journal.
Quote:Original post by Lucidquiet
I've heard its possible, but I don't know the specifics. You can accomplish data hiding (Encapsulation) fairly easilty by with C, but how would someone go about Polymorphism, and Inheritance, with C.


Polymorphism can be emulated via function pointers. Your C++ compiler will create a table containing all function pointers, then create a hidden pointer to that table in the data area of each instance.

Inheritance can be emulated by having structures (the derived type) contain members of other structures (the base types). So each base type will be a part of an instance of the derived type.

Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).
Quote:Original post by nmi
Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).


Thanks, nmi and jonahrowley.

What about structures, for the name mangling problem, I mean if 2 structures have the same function pointers in them, with same names, there shouldn't be conflict I think...

L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Quote:Original post by Lucidquiet
Quote:Original post by nmi
Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).


Thanks, nmi and jonahrowley.

What about structures, for the name mangling problem, I mean if 2 structures have the same function pointers in them, with same names, there shouldn't be conflict I think...

L-


You are right.

On a side note, since "member functions" are implemented as function pointers in C, I don't see why you may need name mangling - just give a diferent name to the new function (C don't allow you to create functiosn with the same name, so it is not really a problem). Did I miss something?

You might also need to play with complex type casting - and in the end, you'll probably end up with something along the line of GLib's GObject, which is pretty heavy.

Regards,
LUA might fit somewhere between C and Python.

But seriously, why do you want to create a new language, and why does that mean you need to know how to use OO facilities in C?
I love to make new languages, its all I ever do when programming. Making interpreters or compilers is bout all I do lol. Im guessing your writing an interpreter/vm for this? If your making a compiler I dont see why you would need to know how to do OO in C...and why dont you just write the interpreter in C++?
What's the goal of your C/Python hybrid?
Quote:Original post by Lucidquiet
Quote:Original post by nmi
Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).

What about structures, for the name mangling problem, I mean if 2 structures have the same function pointers in them, with same names, there shouldn't be conflict I think...


You don't have to mangle names to have structures with members of type pointer-to-function. The mangling comes into play if you want to support things like namespaces or overloading.

An excellent example of object-oriented programming using C is the stdio FILE* functions (fopen(), fread(), fwrite(), ftell(), fclose() etc).

Another example is the entire OpenSSL suite, but not for the faint-of-heart.

Stephen M. Webb
Professional Free Software Developer

The first C++ compiler wasn't really a true compiler even, it was some sort of nice preprocessor that converted the C++ code into C code.

So most standard(i.e., not arcane or overly complex) features of C++ are "emulable " in C, in some form or another. Whether it's nice and clean or horribly messy C, that remains a question.

This topic is closed to new replies.

Advertisement