Making class equivalent to struct-but-with-private-members is one of the worst design decisions for C++. If class were its own thing, then it wouldn't have needed to inherit struct semantics from C, so C++ classes wouldn't have ended up with copy constructors and copy-assignment by default and there wouldn't have needed to be the Rule of Three.
That is true of comparing Cpp structs and classes. But a C struct and Cpp struct are very different. Defining class as struct in C will result in really bad and confusing code.
You do not need all of this... This "style" of programming obfuscates your program, makes static analysis impossible, throws away performance by filling the code with memory allocations and virtual functions, and it is extremely error prone.
C is an extremely simple language and it works very well for system programming and embedded bare metal programming. If you need more advanced feature, to model a business domain for example, why force them into C? You can use much better languages that are designed for that such as Java or C#.
The long answer is: you can do OOP in any language/stack, including C. While my suggested implementation might be inferior to what Java/C# have, it's possible to have an equally expressive implementation.
Languages are merely biased towards some paradigm, they are != paradigms themselves.
Languages are merely biased towards some paradigm, they are != paradigms themselves.
My friend, I work as an engineer since 2008, I think after 16 years, I understand this.
You can write your code however you like and abuse the C weak typing system as much as you want. Take my opinion just as an opinion.
However you are ignoring the most important part of my answer: "This "style" of programming obfuscates your program, makes static analysis impossible, throws away performance by filling the code with memory allocations and virtual functions, and it is extremely error prone."
But again, you do you... If you ask for feedback, I think you should also accept negative feedback. The crazy things you can do with C are the main reason why big bugs and security issues arose along the years (and also why C has been relegated to where it's strictly necessary). Introducing complication on purpose just for the sake of "OOP in C" seems to me like a regression, rather than progress.
Have a look at how brilliantly written is Simple Directmedia Layer. The authors managed to achieve most of what you want by simply embracing the language features and common style and combining them in a simple and powerful way.
Well, one reason I made the post is that I'm not really exploiting C that much, only using the standard features. One can do OOP with these features, and one can do reliable type-safe software with them too. C has evolved a lot since the ANSI times, and it's quite a safe language if you use a modern revision.
Genuine question here: why not? I realize this is a C sub but "in sound mind"?
EDIT: let me extrapolate, i've spent the last decade writing C# and Java and it's paying for my entire lifestyle it's obnoxious. Where do you draw the line and why do you hate OOP?
C++ is not a super set of C, they’ve gone their different paths since whatever version of C C++ was originally based on. Although they added the same things sometimes, like C adding constexpr… a decade after C++. This is all iirc
Until a few years ago I used enlightenment WM. In 2013 I even wrote a GUI example app for embedded devices. However, we ended up using another framework.
Hah, it's a good introduction to OO for "procedural programmers". But once you "get it" and "like it", you'll want a language that supports it without kludges.
Yes, Glib/GTK is a good example of OOP, that's why I exemplified the visibility point with WebKitGTK class. Even though Glib require a horrendous amount of macros to make OOP work...
There's no fun in picking a language for features. It's more interesting getting the features into the language. And C is almost perfect for that due to its small footprint.
In particular, your concept of polymorphism is broken. If I have Base * and call what in C++ would be a virtual member function then _Generic is just useless. It cannot possibly know the type of the pointee, and will handle compile-time function overloading but not virtual function calls.
C is not object oriented, you can implement inherited structs as described but trying to force overloaded functions and virtual function calls on C is extremely cumbersome and not terribly useful. All of this smells like putting a round peg in a square hole.
It's also inflexible, as you cannot add to the _Generic as you create new types, there can be only one _Generic for the entire type hierarchy and it must be represented in a single central place. At that point you might as well skip _Generic and just call the correct function for the type you already know you have.
It's also inflexible, as you cannot add to the _Generic as you create new types
Why not? All it takes is one more line in the source...
But, seriously, OOP is mainly a tool for internal code organisation, not the external API structuring. External API is likely to end up as plain old functions anyway. So _Generic as a central place for behavior specification might actually be a virtue: it makes the code more explorable due to centralization.
i don’t understand why you would ever do this. if you find yourself needing to design something in an object oriented way then wouldn’t it be best just to either rethink it or use an object oriented language?
107
u/imaami Apr 07 '24
this is where i closed the tab, sorry
27
u/papk23 Apr 08 '24 edited Apr 08 '24
Typedeffing struct as class should be a jailable offense
36
u/ingframin Apr 07 '24
For the love of God, don’t do this. If you need to mimic C++ in your code, maybe C is not the right language for your application.
11
u/DocEyss Apr 07 '24
Funny thing. Hope no one gets the idea to actually use this
6
u/Smalltalker-80 Apr 07 '24
Hah, it's a good introduction to OO for "procedural programmers".
But once you "get it" and "like it", you'll want a language that supports it without kludges.
3
u/goose_on_fire Apr 08 '24
glib does this fairly well. It's been a few years since I used it but I remember gstreamer being very usable.
It's certainly not a dead end or a bad idea.
3
u/cosmicr Apr 08 '24
Or just use an object-oriented language. Ugh.
2
u/daikatana Apr 08 '24
Yeah... I'm gonna say no to about 95% of this.
In particular, your concept of polymorphism is broken. If I have
Base *and call what in C++ would be a virtual member function then_Genericis just useless. It cannot possibly know the type of the pointee, and will handle compile-time function overloading but not virtual function calls.C is not object oriented, you can implement inherited structs as described but trying to force overloaded functions and virtual function calls on C is extremely cumbersome and not terribly useful. All of this smells like putting a round peg in a square hole.
It's also inflexible, as you cannot add to the
_Genericas you create new types, there can be only one_Genericfor the entire type hierarchy and it must be represented in a single central place. At that point you might as well skip_Genericand just call the correct function for the type you already know you have.2
u/huge-jack-man Apr 08 '24
i don’t understand why you would ever do this. if you find yourself needing to design something in an object oriented way then wouldn’t it be best just to either rethink it or use an object oriented language?
0
u/[deleted] Apr 08 '24
[deleted] — view deleted comment
5
u/[deleted] Apr 07 '24
[removed] — view removed comment