container_of doesn't really handle dynamic dispatch though, it's a compile time approach to inheritance by performing upcasting through an embedded base structure field. This would still be unsafe for runtime polymorphism without a field tracking the current type or, like done here V-tables, which you likely still want to generate with that approach.
Now whether I like container_of more than this? 100%, in fact, a past post of mine on this subreddit essentially used that
Tracking of dynamic type can be done by checking "ops" struct with function pointers to "virtual functions". It's done very often in Linux kernel i.e. to check origin of file descriptors.
Yes, that's possible with this approach as well. Basically, the container_of approach isn't really an approach, it's V-Tables using container_of as a tool for one-way statically checked dynamic casting.
I'm just arguing that V-Tables are not something I would write manually. I have made a lightweight interface extension (that uses fat pointers instead of object semantics) for C before that I actually use in my personal projects because I find the manual typing of it annoying
It's not that I think that your solution is wrong. It is convenient, it's extensible, but a bit slow due to extra indirection. Usually, the performance difference is negligible for high-level abstractions like UI.
The container_of compiles essentially to "subtracting constant from a pointer", a single line of register-only assembly. Moreover, the macro does type checking, thus it is safer than a plain void*. (btw.. type-safe container_of can be written in C89-compliant way).
This pattern works best for static-inheritance but it lets use type-safe dynamic inheritance if necessary. Thus, imo, it is more suited to low-level and high-performance systems.
Yeah, C# is a great language. When you leave the MS ecosystem you lose the best C# frameworks unfortunately. I tried GTK# and others, but it just wasn't right.
to in this case declare an enum and the string representations at once. X-macro lists can also be passed to other macros, but you would be severely limited by the fact that you can't use preprocessor directives inside of a macro; including a file has no such limitations
It's doing some kind of macro magic to implement the polymorphism. So whatever is in slopjc.h gets reprocessed with the different definitions of SLOPJC each time.
It's kind of error prone. If you mess up one of the commas etc. in the class definitions you will likely end up with near undecipherable errors for the average programmer. Debugging projects using these kinds of techniques can be quite a headache, which is why you would likely avoid it in production.
Though I did specifically focus on keeping it readable and not treading into Gobject levels of unreadable
It was the use of the pet.MakeSound. It was used to show classes and have you could overwrite methods via abstraction. Not sure why it stuck this long. I think it made me laugh.
Oh yeah lol it's just a classic example that popped up when I was looking for an example for the post. I don't actually write in an OOP style so I needed some inspiration lol
and Log and Format only take Objects as arguments and call the ToString virtual method, which String also implements for itself (doesn't copy the object; only bumps the reference count and adds the object to the current AutoreleasePool)
What do you mean? It's just a classic OOP example slightly adjusted to be a little more complicated/interesting, but I wrote the library myself. The title is a joke btw
126
u/JuniorAd1610 Jul 05 '26
I think this this is the standard way they OOPS in C right?
58
u/MatJosher Jul 05 '26
Well they did call it slop. Actual Objective-C accomplishes this much more gracefully. Why not just use it?
33
u/KaleidoscopePlusPlus Jul 05 '26
nice theme. feels good, easy on the eyes.
14
u/steadyfan Jul 05 '26
2 things..
17
u/tstanisl Jul 05 '26
It's not that bad. A bit non-idiomatic but consistent and readable enough.
7
u/Nilrem2 Jul 05 '26
I’m a hobbyist when it comes to C. I think it looks fine, after a few lines I could read it.
Edit: what’s wrong with it?
7
u/gfoyle76 Jul 05 '26
It's oldschool, it's full of macros, kinda unsafe, but hey, as long as it works...
5
u/SoCalChrisW Jul 05 '26
Is this the source code to an old See N' Say toy?
3
u/Maqi-X Jul 05 '26
source code please??
3
u/DesiresQuiet Jul 05 '26
Nearly this exact code was in a book on learning C and classes from like 1989.
2
u/r9wpvM Jul 10 '26
Wait is String a thing in C I don't remember
3
u/Professional_Bat2629 Jul 05 '26
What is your font? Looks cool 😊
10
u/richardathome Jul 05 '26
More people in this thread commenting on the font and not the code.
I approve. That font makes shit look good!
4
u/richardathome Jul 05 '26
Thanks. I hate it.
1
u/Cheefbird Jul 07 '26
I don’t believe you. This reads as a tutorial on OOP.
Edit: if you swapped classes to cats and dogs then yes, I’m an idiot.
1
u/markand67 Jul 08 '26
I've seen way, way, way in order of magnitude horror in our current german codebase.
2
u/EmelineRawr Jul 05 '26
Real slop 😭