Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

If you were to extend C with some OO elements

Name: Anonymous 2009-12-02 16:50

Not that there would be much point doing that and not just using a different language.
But I thought a little about things like classes with single inheritance (achieved by replicating the same code in the inheriting class as in the base one), and I wonder about ways to implement virtual functions. I thought that for each vfunction you could just store its pointer as an additional member, but then with multiple members a vtable would make for more efficient storage. And also, I could replace the vtable pointer with a pointer to some kind of a type descriptor (which would then have the vtable), and add some type of reflection.
But the latter two methods would have to add some kind of a ``hidden'' field to the struct, while in the first one the count and location of those fields would be predictable. If I went with the ``hidden pointer'' approach, I would then need to adjust pointers behind the scenes and some other shit. I wonder how C++ does this?

Also, your thoughts. And keep in mind that I'm not some kind of EXPERT LANGUAGE DESIGNER (at least not yet), so don't get too mad.

Name: Anonymous 2009-12-03 4:35

>>14
I'm not >>13, but I did implement my own OO with inheritance in about 3 pages of C code. Add a few macros there for a more manageable syntax, and that becomes 4 pages of code. It's really simple once you understand what exactly are "objects" and "classes", and how "inheritance" works.

Methods are functions which operate on objects and other data. Objects are nothing more than structures. If you want to implement message-passing OO (instead of multi-dispatch OO), all you have to do is have add a vtable to the object upon creation. A vtable is an array of virtual functions belonging to that class. You may add a few fields for metadata such as object's size, or tag, or even structure if you want to go that far. Since you're writing this in C, you probably don't want to go too far with the feature-set, otherwise it may become a bit too tedious to manage, even with some macros to write the function calls for you. I prefer to keep my "OO" thinking in C, just at the "functions manipulation (possibly tagged) structures of some type" phase.



If you're using a more expressive language like Lisp, you can implement a minimal message passing system in less than a page, even though CL already has CLOS+MOP, which is likely the most advanced OO system I've seen so far, maybe even a bit too advanced for regular usage, but at least it doesn't tie methods to a single class, instead (multi-)methods belong to a generic method of one signature, and you specialize the arguments on specific classes/types, which allows one a much greater flexibility and freedom than the usual message passing system. Such freedom is not unlike doing OO in C, but it allows you to hide details under abstractions much better.


Which style one prefers is a matter of personal taste.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List