static_cast

A

asit

static_cast is used to convert between enum and integer.

the same can be done using c-type casting.

then why static_cast is used ??
 
N

Nick Keighley

static_cast is used to convert between enum and integer.

the same can be done using c-type casting.

then why static_cast is used ??

a c-style cast can do a lot of things. By using a static_cast you are
expressing your intent more clearly. The "new" style casts are easier
to find with grep and standout in the code. Casts should be few and
far between. They can be the source of subtle (and not so subtle)
bugs. The ability to easily find all the casts in your source code can
be helpful.

char s;
strcpy ((char*)s, "hello");
 
G

Goran

static_cast is used to convert between enum and integer.

the same can be done using c-type casting.

then why static_cast is used ??

1. C-style casting casts *everything* away, whereas C++-style cast
offers more control.
2. C++-style casts are much easier to find in sources; that's
important, see 3.
3. Casting is a sign of a poor design more often than not. Because C++-
style cast is pain to read and write, it promotes thinking and design
over quick hacks ;-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top