But what's the major difference between static_cast and C style casting? Or,
why there is a static_cast?
[]s
Fred
A static_cast<B>(A) will do a conversion from any type "A" that is
converitble to "B". This means it can use conversion operators or
constructors(?) to do the casting, whereas a C-style cast won't.
All the casts have their jobs to do. C-style casts are best avoided where
possible in C++ for two reasons:
1. They are difficult to find. It is easier to search for _cast than
just a couple of parentheses that are overloaded with all sorts of other
meanings.
2. C++ casts have narrower meanings and so show the programmer's intent
more.
A dynamic cast is only "safer" in at run-time you get a NULL pointer or a
std::bad_cast exception if the type can not be cast. In one way, a
static_cast is safer because the compiler will tell you whether the cast is
legal rather than wait to find out at runtime. Always prefer static_cast to
dynamic_cast. (That doesn't mean avoid dynamic_cast, just prefer
static_cast.)
S.
dynamic_cast does do type checking - and can only be sensibly applied to
pointers and references to polymorphic types.
>
> But what's the major difference between static_cast and C style
> casting? Or, why there is a static_cast?
A C-style cast could be a static_cast, a const_cast, a reinterpret_cast, or
some combination of the above. They were separated in C++ to give the
programmer more flexibility to state explicitly why something is being cast
(e.g. const_cast can only add or remove const (or volatile)), while a
static_cast can only cast between types with an existing implcit or explicit
conversion.
-cd
Will too:
struct C {
C(int) {}
};
int main() {
C c(1);
c = (C)2; // C-style cast calling constructor
return 0;
}
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
By using a C-style casting, you tell the compiler to forget about
type mismatches and to do a casting whatever possible. This style
is prone to errors in run-time. Though it is shorter and may be
preferable in particular cases (int to double, ...).
C++ style casters are more intellectual and intuitive.
dynamic_cast does 'nothing' with static_cast. dynamic-cast is for
polymorphic type casting (via pointers or references). if fails, it will
return 0.
reinterpret_cast is for cases when you cannot apply one of
static/dynaic/const casters. It also returns 0 if fails.
--
======
Arman
... or throw bad_cast, if the cast involved references rather than
pointers.
> reinterpret_cast is for cases when you cannot apply one of
> static/dynaic/const casters. It also returns 0 if fails.
reinterpret_cast never fails at run-time. Only dynamic_cast has well
defined failure mode.
Thanks!
[]s
Fred
Thank you for the very good explanation on the subject... I'm an old-style
programmer and don't use C++ casting mechanism... But I'm starting to do
so... and had some little doubts...
[]s
Fred
Your follow-up post is correct, as always.
S.