3 ms·
I agree that you can come very close to C++ with C. (After all, the original C++ implementation was cfront, which was just a C preprocessor). But to me, the d
by AnimalMuppet 2y ago
I agree that you can come very close to C++ with C. (After all, the original C++ implementation was cfront, which was just a C preprocessor).
But to me, the difference between C++ and what you're saying is this: If you say
typedef struct {
// Private stuff, do not rely on it
int bar;
float baz;
} foo_ctx;
The only thing defending bar and baz from my meddling is the comment. If you're in C++ and you make bar and baz private, then I can't modify (or even read) them without going through the interfaces you design. The larger the codebase, the larger the team, and the more average-or-below coworkers you have, the more that matters.
- loup-vaillant 2y agoYou're correct. Which is why in practice I often consider using a forward declaration and pointer to implementation instead. But you know what, even that can be gamed. I have gamed it once: I copied & pasted the definition of the struct, and accessed its member from a place that was supposed to only have access to an opaque pointer. It worked. (To be fair, this isn't the kind of stuff you do by mistake, unlike merely accessing a struct member.)