4 ms·
Object oriented programming in C (2007)
- memco 9y agoCasey Muratori recently gave a great talk during a Q&A talking about the way he uses C to achieve some of the same things people typically think of as object oriented (starts around 19:39)[0]. It doesn’t cover everything that makes up OO design but I like it because highlights how C is powerful and capable of things like polymorphism in a cleaner way than C++. [0]https://hero.handmade.network/episode/chat/chat014#1179 https://hero.handmade.network/episode/chat/chat014#1179
- unkown-unknowns 9y agoDirect link to video at 19:39 https://www.youtube.com/watch?v=EhtxDXlrJ6Y&t=19m39s https://www.youtube.com/watch?v=EhtxDXlrJ6Y&t=19m39s
- deleted 9y ago[deleted]
- thisisadumb 9y agojust use c++ dude
- novium 9y agoThere are still a few reasons to use C even to this day and I'd add that the page was last updated in 2007.
- CyberDildonics 9y agoUnless it's compile speed or not having an updated C++ compiler for your platform, I'm not sure what that would be. Edit: Is it the new thing to hate on C++? Downvotes aren't arguments
- wruza 9y agoIt was never a new thing, afaicr.
- flavio81 9y ago>just use c++ dude Instead of choosing the OOP system that C++ forces on you; you can just use C and choose which OOP system to use.
- euyyn 9y agoI bet you can still probably build it and use it more easily with C++ than with C.
- catnaroek 9y agoThis isn't surprising. All that you need to write object-oriented programs (not necessarily in a convenient manner) is some form of dynamically dispatched procedure calls.
- theyregreat 9y agoYup. Pointers to functions and call equivalence of functions and pointers to functions are that in C. Prototype interfaces (ie plugins) are trivially had with a struct containing pointers to such common API functions. The plugin just needs an init function that returns a pointer to such a const structure, and you’re in business. Another popular trick is to use opaque structs for private data contained in the public struct in the header. Then, the private code defines the private struct. It’s not perfect information hiding but it separates what is intended public and what is intended to be private. /* foo.h */ #ifndef FOO_H #define FOO_H typedef struct _foo_ctx_t { int timeout; void *priv; } foo_ctx_t; #ifdef _cplusplus extern “C” { #endif foo_ctx_t *foo_init(foo_ctx_t *self); int foo_bar(foo_ctx_t *self); #ifdef _cplusplus } #endif #endif /* FOO_H */ /* foo.c */ #include “foo.h” #include <stdlib.h> #define PRIVATE(var) ((foo_private_ctx_t *)(self->priv)->(var)) typedef struct _foo_private_ctx_t { int whatever; } foo_private_ctx_t; foo_ctx_t * foo_init(foo_ctx_t *self) { self->timeout = 1000; self->priv = malloc(sizeof(ctx_private_ctx_t)); if (!self->priv) return NULL; PRIVATE(whatever) = 0; return self; } int foo_bar(foo_ctx_t *self) { PRIVATE(whatever) += self->timeout; return 1; }
- teporxahor 9y agoThere's also the book from Axel T. Schreiner: https://www.cs.rit.edu/~ats/ https://www.cs.rit.edu/~ats/