Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between static_cast and C-Style casting

49 views
Skip to first unread message

Frederico Pissarra

unread,
Apr 6, 2006, 9:16:51 PM4/6/06
to
I understand that dynamic_cast is safer than static_cast... and dynamic_cast
do some sort of "type checking"...

But what's the major difference between static_cast and C style casting? Or,
why there is a static_cast?

[]s
Fred


Simon Trew

unread,
Apr 6, 2006, 10:09:52 PM4/6/06
to
A C-style cast is (with some subtleties) either a reinterpret_cast or a
static_cast.

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.


Carl Daniel [VC++ MVP]

unread,
Apr 6, 2006, 10:13:02 PM4/6/06
to
Frederico Pissarra wrote:
> I understand that dynamic_cast is safer than static_cast... and
> dynamic_cast do some sort of "type checking"...

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


Igor Tandetnik

unread,
Apr 6, 2006, 10:33:07 PM4/6/06
to
"Simon Trew" <ten.enagro@werts> wrote in message
news:uOMpbheW...@TK2MSFTNGP03.phx.gbl

> 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.

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


Arman Sahakyan

unread,
Apr 7, 2006, 2:58:01 AM4/7/06
to
Hi,

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

Igor Tandetnik

unread,
Apr 7, 2006, 7:55:52 AM4/7/06
to
"Arman Sahakyan" <arman...@rambler.ru(donotspam)> wrote in message
news:BB92D370-F2CB-4193...@microsoft.com

> dynamic_cast does 'nothing' with static_cast. dynamic-cast is for
> polymorphic type casting (via pointers or references). if fails, it
> will return 0.

... 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.

Frederico Pissarra

unread,
Apr 7, 2006, 11:44:36 PM4/7/06
to

"Carl Daniel [VC++ MVP]" <cpdaniel_remove...@mvps.org.nospam>
wrote in message news:OjsiNjeW...@TK2MSFTNGP04.phx.gbl...

Thanks!

[]s
Fred


Frederico Pissarra

unread,
Apr 7, 2006, 11:43:23 PM4/7/06
to

"Simon Trew" <ten.enagro@werts> wrote in message
news:uOMpbheW...@TK2MSFTNGP03.phx.gbl...

>A C-style cast is (with some subtleties) either a reinterpret_cast or a
>static_cast.
>
> 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.

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

Simon Trew

unread,
Apr 10, 2006, 8:54:49 PM4/10/06
to
As usual, Igor, I failed to find the right compromise between brevity and
absolute correctness.

Your follow-up post is correct, as always.

S.


0 new messages