Can you write object-oriented code in C? Especially with regard to polymorphism.
See also Stack Overflow question Object-orientation in C.
Can you write object-oriented code in C? Especially with regard to polymorphism.
See also Stack Overflow question Object-orientation in C.
See http://slkpg.byethost7.com/instance.html for yet another twist on OOP in C. It emphasizes instance data for reentrancy using just native C. Multiple inheritance is done manually using function wrappers. Type safety is maintained. Here is a small sample:
I propose to use Objective-C, which is a superset of C.
While Objective-C is 30 years old, it allows to write elegant code.
http://en.wikipedia.org/wiki/Objective-C
One thing you might want to do is look into the implementation of the Xt toolkit for X Window. Sure it is getting long in the tooth, but many of the structures used were designed to work in an OO fashion within traditional C. Generally this means adding an extra layer of indirection here and there and designing structures to lay over each other.
You can really do lots in the way of OO situated in C this way, even though it feels like it some times, OO concepts did not spring fully formed from the mind of
#include<favorite_OO_Guru.h>
. They really constituted many of the established best practice of the time. OO languages and systems only distilled and amplified parts of the programing zeitgeist of the day.Since you're talking about polymorphism then yes, you can, we were doing that sort of stuff years before C++ came about.
Basically you use a
struct
to hold both the data and a list of function pointers to point to the relevant functions for that data.So, in a communications class, you would have an open, read, write and close call which would be maintained as four function pointers in the structure, alongside the data for an object, something like:
Of course, those code segments above would actually be in a "constructor" such as
rs232Init()
.When you 'inherit' from that class, you just change the pointers to point to your own functions. Everyone that called those functions would do it through the function pointers, giving you your polymorphism:
Sort of like a manual vtable.
You could even have virtual classes by setting the pointers to NULL -the behaviour would be slightly different to C++ (a core dump at run-time rather than an error at compile time).
Here's a piece of sample code that demonstrates it. First the top-level class structure:
Then we have the functions for the TCP 'subclass':
And the HTTP one as well:
And finally a test program to show it in action:
This produces the output:
so you can see that the different functions are being called, depending on the sub-class.
Sure that is possible. This is what GObject, the framework that all of GTK+ and GNOME is based on, does.
The C stdio FILE sub-library is an excellent example of how to create abstraction, encapsulation, and modularity in unadulterated C.
Inheritance and polymorphism - the other aspects often considered essential to OOP - do not necessarily provide the productivity gains they promise and reasonable arguments have been made that they can actually hinder development and thinking about the problem domain.