2 ms·
> void * implicit casting in C just works, but in C++ it must be an explicit cast In C, casting a `void *` is a code smell, I feel. Most confusing one is how
by lelanthran 1y ago
> void * implicit casting in C just works, but in C++ it must be an explicit cast
In C, casting a `void *` is a code smell, I feel.
Most confusing one is how the meaning of `const` differs between C and C++; I'm pretty certain the C `const` keyword is broken compared to `const` in C++.
- 1718627440 1y agoYes, casting in general is a code smell. (Not as in never use it, but as in use it as less as possible of course.) The thing is, void was introduced exactly to represent this use-case without casting. The type to represent random amount of bytes, check which type and then cast is called char *. void * is for when you know exactly which type it has you just need to pass it through a generic interface, which isn't supposed to know or touch the type. The other use case is for when the object hasn't a type yet, like from malloc. Previously everything was typed char *, then void * was used to separate the cases: casting from char *, don't cast from void *. Now in C++ both are the same again.