Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11554 > unrolled thread

static_cast

Started byasit <lipun4u@gmail.com>
First post2011-11-17 00:16 -0800
Last post2011-11-17 02:48 -0800
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  static_cast asit <lipun4u@gmail.com> - 2011-11-17 00:16 -0800
    Re: static_cast Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-17 01:28 -0800
    Re: static_cast Goran <goran.pusic@gmail.com> - 2011-11-17 02:48 -0800

#11554 — static_cast

Fromasit <lipun4u@gmail.com>
Date2011-11-17 00:16 -0800
Subjectstatic_cast
Message-ID<11035735.3.1321517768926.JavaMail.geo-discussion-forums@prim23>
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 ?? 

[toc] | [next] | [standalone]


#11556

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2011-11-17 01:28 -0800
Message-ID<cfe778a6-9fc7-4694-a7c5-05ab2e3323c2@d17g2000yql.googlegroups.com>
In reply to#11554
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");

[toc] | [prev] | [next] | [standalone]


#11561

FromGoran <goran.pusic@gmail.com>
Date2011-11-17 02:48 -0800
Message-ID<fdc727b1-ab68-4d2d-a9f4-595fd7598b37@r9g2000vbw.googlegroups.com>
In reply to#11554
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 ;-).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web