Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   static_cast (http://www.velocityreviews.com/forums/t806016-static_cast.html)

asit 11-17-2011 08:16 AM

static_cast
 
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 ??

Nick Keighley 11-17-2011 09:28 AM

Re: static_cast
 
On Nov 17, 8:16*am, asit <lipu...@gmail.com> 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");

Goran 11-17-2011 10:48 AM

Re: static_cast
 
On Nov 17, 9:16*am, asit <lipu...@gmail.com> 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 ;-).


All times are GMT. The time now is 02:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.