David Kleinecke <
dklei...@gmail.com> writes:
> C is not an object oriented language (see C++). But object-
> orientation is an organizing philosophy and a set of rules.
> In OO organization the focus is on the data - rather than
> the code. In classic C it was not quite clear where the
> persistent data lived. One reading would be that it lived
> in the set of top-level declarations in a translation unit
> (and in static declarations in functions). But that reading
> remains unsatisfactory because it cannot handle more than
> one set of data at a time.
>
> Suppose I wish to simultaneously run two instances of the
> same program with different data. Unless I take special pains
> I must load the code for the program twice (for well-known
> reasons). The special pains I must take can be object
> orientation.
>
> The C compatible solution to the problem of more than one set
> of data sharing the same code is:
> Collect all the data into a struct,
> Create instances of that struct on the heap,
> Set the first variable of each of the methods (functions
> making up the code) to a pointer to an instance.
> It is easy enough to add details here like having struct
> members be pointers to further structs.
There are countless systems to add Object Orientation to C.
Just use one of them!
The most famous ones are Objective-C and C++, but we can agree that at
least C++ forked into an entirely different programming language, so
let's discount it.
Nonetheless, you could consider Objective-C (not only on Apple systems,
it's included in gcc and clang), or some other systems, more or less
hacky, you can find on github and elsewhere.
Notice that the problems you have when you want to load two different
programs in the same process, or multiple instances of the same program
in the same process, are not really solved by OOP, when you still allow
global variables and singleton objects. (And all programs have singleton
objects, if only the object that represent the program, which is often
confused with the object that represents the interface with the system
in object oriented frameworks (the so called "Application" class).
Here you have another solution: just keep each of your object in a
separate process, and implement the message sending between object as an
IPC. You get CORBA (but also dbus, rmpi and numerous other solutions).
ie. there are already solutions.
--
__Pascal J. Bourguignon
http://www.informatimago.com