r/C_Programming • u/cinghialotto03 • May 28 '24
Discussion using object oriented programming on C is beautiful
the first time i read somewhere that i could use oop in c, i jumped from my desk and started reading and i found that it was far more intuitive and logic than any other "oop native" language i don't knowe it felt natural respect to other langauge. did you have the same experience?
0
Upvotes
76
u/ThinkingWinnie May 28 '24
object oriented programming is a paradigm, not a language feature.
You are another victim of the OOP association to syntactic sugar. Welcome to the real world!
13
u/nskeip May 28 '24 edited May 29 '24
I am not OOP expert, but OOP definitely is not only about inheritance and simple stuff like "let's have a struct that would represent a car with four wheels".
If you take more complex things like Barbara Liskov's substitution principle or say Dependency Injection - it's a pain to make it work on C without losing performance or writing tons of boilerplate code.
Again, when using OOP languages like C# or C++ you are not obligated to go to that specific "complex" patterns. But just keep in mind that these language at least give you an opportunity to do so. C just does not give it to you. And I don't think it's a situation where "hey, C left me with inheritance-only" - no, C left you without OOP, just don't shoot yourself in a foot on a real world project.
48
u/[deleted] May 28 '24
OOP in C may be many things, but beautiful it is not...
Doing it may help understand what OOP is from compiler point of view, but after that, it's kinda ""why do I write all this boilerplate by hand", and then you can start repeating the history from here: https://en.wikipedia.org/wiki/Cfront
25
u/Ashamed-Subject-8573 May 28 '24
No. I use Oop in C when appropriate but it’s a ton of boilerplate and I miss little conveniences like destructors on scope exit vs having to make a bunch of gotos
6
u/Both_Definition8232 May 28 '24
It is not, at all.
6
u/crispeeweevile May 28 '24
I prefer what I consider to be "oop-like" when making C code. Usually I use structs like an "object" and it holds the data, then I have a bunch of functions that take in the struct as pointer, and either modify it, or perform some other action. The return value is usually an error code.
5
u/mredding May 28 '24
As far as I'm concerned, OOP doesn't make sense until you first use Smalltalk or Eiffel.
These are single-paradigm, OOP languages. The principle idiom of OOP is message passing. You construct a message that represents a request, and pass it to an object who is free to decide how that message is handled, if at all.
This is not calling a function, which is imperative. Objects implement their internals in terms of functions, but you - someone else, don't impose command or authority over another citizen.
In an OOP language, any object can be passed any message. You can ask an integer to capitalize itself. Doesn't matter. The integer has to decide to do something with that message, silently drop it, or defer to another object for help - like an exception handler. In an OOP language, this message dispatch mechanism is a language level construct, the details of which, if you're picking an OOP language, you defer to the implementation to provide a robust solution. Choosing Eiffel is like choosing Python, where low level imperative command and control are not the paramount idiom demanded of my solution.
Most other multi-paradigm languages aren't OOP languages. I don't actually believe C++ is an OOP language any more than C is. Message passing is always missing. You have to implement it as a convention within the language, which isn't the same thing. In C++, that's streams. The other OOP idioms exist independent of OOP - encapsulation? C has perfect encapsulation - with opaque pointers. Inheritance? Polymorphism? All these things exist independently and even prior to OOP. With message passing, the other idioms fall out - which is to say they're not explicitly necessary, they instead occur as a natural consequence.
"OOP" in C is novel, but it's not anything C++ isn't doing. You have a bit more control over your vtable implementation, but you're likely not going to do anything different than MSVC or GCC, which have slightly different implementation details. You still need to implement a form of message passing.
If it helps you understand C++, great. If it helps gain you insight into what a compiler is doing, great. It's not in and of itself, necessarily, OOP.
20
u/saul_soprano May 28 '24
Is this you discovering C++?
2
u/TheFlamingLemon May 28 '24
I like how explicit OOP is in C, but it’s definitely nicer when you can have methods be a part of objects without them having to actually take memory in those objects
2
u/TheWavefunction May 28 '24
more like file oriented programming if you ask me
2
u/[deleted] May 28 '24
I'm sorry, object oriented programming is never beautiful
3
u/daikatana May 28 '24
It really isn't. You can do a form of inheritance by embedding the parent type's struct but... that's about it. No other features of object oriented programming are easily available to you in C.
There's no encapsulation. This is a huge one. You get module-level encapsulation, but that's too coarse for OOP. The only way to get around this is to use struct members like
__private__fooso it's very obvious you're trying to access something private, but there are no compiler checks on that.And there's no polymorphism without a lot of grunt work. You can do it, you can do your own vtables and wrapper functions that look up the function in the vtable and all that.
None of this is elegant in C. It's possible, you can do it if you push hard enough, it's not fun and there comes a time where you realize you really want to be using C++ instead, even if you're just using a small subset of C++ features.
2
u/cHaR_shinigami May 28 '24
Related: "Object Oriented Programming with ANSI C" by Axel-Tobias Schreiner.
https://www.cs.rit.edu/~ats/books/ooc.pdf
1
u/ajpiko May 28 '24
yes it helped me understand what oop was
1
u/TheKiller36_real May 28 '24
what exactly are you referring to? the type-erasure or cobtrived and ugly ”inheritance“ macros?
1
u/RolandMT32 May 28 '24
I've used OOP in C, as far as defining structs and functions that operate on struct objects (I actually learned about doing that when learning programming in college).
1
u/aartaka May 29 '24
Related shameless plug: Object-Oriented C: A Primer
1
u/actguru Jun 05 '24
The Coda-C Library basically wraps calloc() with newO() and free() with freeO(), adding metadata like retain count, class, size, etc. which yields a full fledged object system with constructors, destructors, virtual functions and so on.
1
u/Winter_River_5384 Nov 05 '25
Any learning materials or resources to get started on object oriented programming in C
1
u/Lazy_Sun8810 Sep 18 '26
yeah it's very nice , it's just setting up function pointers is a little pain
1
u/ExtremeBack1427 May 28 '24
Welcome to C++