H
Howard
Hi,
I am maintaining a lot of code that is rife with C-style casts. I've
seen a lot of comments that one should not use C-style casts at all. But
I'm wondering what harm there could be in doing so with the built-in types.
For example, if you have a long which needs to be passed to a function as an
unsigned long, or a char* that needs to be passed as an unsigned char*, or
an unsigned int that needs to be passed as a long, isn't it perfectly safe
to use C-style casts just to avoid compilation problems? When we make
release builds, we generally treat warnings as errors, so there's a lot of
casting going on, and it's a lot less typing to say, for example, (given an
unsigned char* aName): (char*)aName than it is to say
static_cast<char*>(aName). One's 12 characters, the other's 25! That's a
lot of extra work, and what for, exactly? What could go wrong with the
C-style cast in simple cases like this? I know reasons for when I'm dealing
with classes, unrelated pointer types, etc., but what about the simple
built-in types...is there *really* any difference?
Thanks,
-Howard
I am maintaining a lot of code that is rife with C-style casts. I've
seen a lot of comments that one should not use C-style casts at all. But
I'm wondering what harm there could be in doing so with the built-in types.
For example, if you have a long which needs to be passed to a function as an
unsigned long, or a char* that needs to be passed as an unsigned char*, or
an unsigned int that needs to be passed as a long, isn't it perfectly safe
to use C-style casts just to avoid compilation problems? When we make
release builds, we generally treat warnings as errors, so there's a lot of
casting going on, and it's a lot less typing to say, for example, (given an
unsigned char* aName): (char*)aName than it is to say
static_cast<char*>(aName). One's 12 characters, the other's 25! That's a
lot of extra work, and what for, exactly? What could go wrong with the
C-style cast in simple cases like this? I know reasons for when I'm dealing
with classes, unrelated pointer types, etc., but what about the simple
built-in types...is there *really* any difference?
Thanks,
-Howard