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 3:34

>>1
I use C rather than C++ precisely because it *doesn't* hide these things. Inheritance and vtables are very easy to implement, add very little additional code, and conversions to superclasses is explicit. There are also far fewer restrictions; for example you can choose whether to include bases as pointers, and calling constructors explicitly allows you to construct base classes and members conveniently in real C code. This makes stuff like multiple inheritance and virtual bases easy to implement and even easier to understand.

On the other hand, C++ makes everything implicit and hidden. It's is absolutely full of *gotchas*, because you can't just see where vtable pointers are set, or what order virtual base classes are initialized in; you have to figure it out yourself on paper to see what's going on (not to mention actually understand and remember the rules, no small feat). Constructors are horribly broken mainly due to confounding allocation with initialization; initializer lists force you to use a turing-incomplete bastardization of actual C++ syntax, without even so much as local variables. Not to mention all the duplicate and incomplete syntax; for instance the new member function syntax duplicates the C style of taking the struct as the first argument, and in doing so loses support for multiple dispatch.

If you were to extend C with some OO elements
C *does* have OO elements. They work better than the ones in C++. Structs and function pointers are all you need for real inheritance and polymorphism; you don't need compiler ``help'' to accomplish it, especially when that help actually makes things worse.

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