4 ms·
It seems like I've seen this type of C-bashing (vs. C++) recently, and I don't understand it. How is well-written modern C less type-safe than C++? I define t
by scintill76 12y ago
It seems like I've seen this type of C-bashing (vs. C++) recently, and I don't understand it. How is well-written modern C less type-safe than C++? I define that as something along the lines of "using the latest language specification, making judicious use of all features possible, turning on extra-strict compiler warnings and errors."
To address your specific scenario, you could just as well cast a void* to seagull_t* in C++ as you can in C, can't you? I'll admit to not being totally up-to-snuff on the different casts available in C++, but IIRC C-style casting still works.
- to3m 12y agoIt's much easier to do in C, as no cast is required.
- scintill76 12y agoI didn't realize that distinction, so it's good to know. Still, as I originally assumed, I confirmed that gcc with `-Wall -Werror -std=c99` flags won't compile an initialization of a "struct seagull * " with a value of type "struct enemy * ", or passing a mismatch of those types to a function. If those would be the main reasons to use C++, I'd personally rather just use C with extra checks. But to be fair, maybe that's because I'm not very comfortable with C++, to the point that I didn't notice it checks pointer types.
- to3m 12y agoYes. In C, conversion between object pointer types requires a cast, but conversion from void pointer to object pointer does not; in C++, both conversions require a cast. Conversion from void pointer to object pointer would be very a bit annoying to warn about in general, as it's so commonly used in C. And if you always use casts, that also hide other types of error (see, e.g., http://c-faq.com/malloc/mallocnocast.html http://c-faq.com/malloc/mallocnocast.html). I'm sure there are compilers (and possibly gcc/clang can do it, too...) that will warn you about this stuff without needing you to switch to C++.