Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static_cast

Reply
Thread Tools

static_cast

 
 
asit
Guest
Posts: n/a
 
      11-17-2011
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 ??
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      11-17-2011
On Nov 17, 8:16*am, asit <(E-Mail Removed)> wrote:
> 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");
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      11-17-2011
On Nov 17, 9:16*am, asit <(E-Mail Removed)> wrote:
> 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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is static_cast<int>(static_cast<double>(a)) == a? Bo Peng C++ 11 10-20-2006 12:59 PM
Re: static_cast question Alan Sung C++ 0 08-02-2003 02:47 AM
static_cast<> alg C++ 2 07-14-2003 06:01 PM
how to static_cast<MyClass* &>(p) ??? Wenjie C++ 4 07-12-2003 03:04 AM
static_cast confusion Chandra Shekhar Kumar C++ 7 06-27-2003 02:54 PM



Advertisments