9 ms·
Hi. For background, I have been writing professional OOP code in ANSI C. Yes, Structures in C can be used as classes. And yes, this includes polymorphism. My
by twawaaay 4y ago
Hi. For background, I have been writing professional OOP code in ANSI C.
Yes, Structures in C can be used as classes. And yes, this includes polymorphism.
My solution was to put a callback in the structure that would point to the implementation that was polymorphic. This allowed me to pass the struct around and then call the specific implementation at the right time.
When you write OOP in C you do not need to make it look like OOP language. You just need to understand what OOP is about and use the existing language to do what you need it to do as efficiently as possible.
As to spending time on writing functionality that is readily available in other languages... sometimes you just don't have a choice. There are programming environments where ANSI C is the only language available as was in my case.
Please, try not to be too dismissive to the things you do not understand.
- zabzonk 4y agoplease post an example of what such a callback would look like
- rramadass 4y agoOne example: https://www.codementor.io/@michaelsafyan/object-oriented-programming-in-c-du1081gw2 https://www.codementor.io/@michaelsafyan/object-oriented-pro...
- cihangir 4y agoLinux kernel code makes heavy use of object-oriented design patterns [0]. It's not the best of its kind, but still provides a reasonable example of how OOP can be achieved in C: https://gist.github.com/cakturk/cd75d0ca588151c86d641cb6d5a18153 https://gist.github.com/cakturk/cd75d0ca588151c86d641cb6d5a1... [0] https://lwn.net/Articles/444910/ https://lwn.net/Articles/444910/
- rramadass 4y agoPart 2 to the above lwn article: https://lwn.net/Articles/446317/ https://lwn.net/Articles/446317/
- throw_m239339 4y ago> Please, try not to be too dismissive to the things you do not understand. I never said it's impossible to do method polymorphism in C, I said nowhere in that article method polymorphism is implemented. You obviously did not read what I wrote at first place, only saw what you wanted to see to make some kind of (bad) rebuttal without a single concrete implementation as an example. Of course it's possible to implement method polymorphism in C, which is what I said, the article linked isn't doing that anywhere, quite the opposite. So no, C structs are not equivalent to Java classes, at all.
- kitd 4y agoI said nowhere in that article method polymorphism is implemented. Well, not quite. You said that it isn't OOP without method polymorphism. So no need to fly off the handle and accuse others of misreading something you didn't write.
- throw_m239339 4y ago> Well, not quite. You said that it isn't OOP without method polymorphism. So no need to fly off the handle and accuse others of misreading something you didn't write. I said > This isn't OO until you implement some form of method polymorphism. which means the article didn't implement method polymorphism. > So no need to fly off the handle and accuse others of misreading something you didn't write. So you need to pay attention at first place instead of "flying off the handle" yourself and then accusing others to do so in a useless comment.
- lll-o-lll 4y agoYes, the article is terrible as it does not show any polymorphism. Also, yes, doing genuine polymorphic OO in C ends up horrible because you invent a new language. (Source: many years working on a truely evil code-base of OO C. Originally the work of one twisted genius. Brilliant, but evil). OO was just a failed paradigm anyway, I’m amazed anyone would try and shoe-horn it into C in the modern day.
- isityouyesitsme 4y ago
- meindnoch 4y ago>My solution was to put a callback in the structure I think you mean function pointer. But yeah, that's the usual way to do it, although a bit wasteful in terms of memory compared to a vtable (but needs one less indirection to jump to the method).
- twawaaay 4y agoA function pointer is the mechanism by which you realise a callback. For me, a callback, is when you have two modules and the function pointer is used to call back the functionality from the calling module in an environment and at a time determined by the called module. A polymorphic method is a callback used in a certain way in context of OOP. You have a callback from one module (the implementation) passed to another module that can call potentially many different implementations through the same interface (which makes it polymorphic). This callback is tied to the structure by the calling module, which makes it a method. You may have language support and have entire mechanism hidden from you, but this is essentially what is happening. A pointer to function that is passed to another module for execution. Now, when you do "manual" OOP in C without building your own vtable macros and such, I think "polymorphic method" is a bit too much and "callback" is more fitting.