Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

static_cast etc

 
 
shrishjain@gmail.com
Guest
Posts: n/a
 
      03-14-2005
Hi All,

Do people frequently use static_cast, const_cast etc in industry?.. I
only saw them in books, and never in real code..

Shrish

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-14-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed):
>
> Do people frequently use static_cast, const_cast etc in industry?.. I
> only saw them in books, and never in real code..


Then you haven't seen real code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Larry Brasfield
Guest
Posts: n/a
 
      03-14-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi All,

Greetings, one.
> Do people frequently use static_cast, const_cast etc in industry?.. I
> only saw them in books, and never in real code..



I've seen it used in "industry" and real code.
I have also seen people who were not yet
using them become convinced that it was
a good idea to begin.

If you are working in shops where nobody
has seen the virtues of modern C++ casts,
or where they are too stubborn to adopt
them, then you might want to consider a
few changes. This may be one of many
opportunities to lead rather than follow.

--
--Larry Brasfield
email: (E-Mail Removed)
Above views may belong only to me.


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      03-15-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi All,
>
> Do people frequently use static_cast, const_cast etc in industry?.. I
> only saw them in books, and never in real code..
>
> Shrish
>


To be honest, I still have trouble forcing myself to use static_cast instead
of the old C-style cast, since I've been doing it the old way for so long.
But if a cast is neccessary, and the proper cast for the situation is
static_cast, then your only other choice is a C-style cast, right? And
that's a worse option, not a better one.

So the question is, where is static_cast needed?

Well, one place I need it often is to force my mathematical calculations to
use a specific numeric type. For example, if I'm dividing two integers, but
I need a floating-point result, I need to either use a cast, or else assign
one of those values to a temporary float (or double) variable. A cast is
easier. (If I don't do this, then the division is integer division, which
loses the real portion of the answer!)

The other place I use it often is in callback functions (esp. when dealing
with the Windows API). In those functions, there is often a void* pointer
that is passed to my function, which I set up however I see fit when I
specify the callback to the OS. Usually I set the pointer to the value of
"this", so that the object who needs to be "called back" is identified. The
callback function then uses a static_cast to turn that void* into a pointer
to a pointer to my class type, so that I can then call a member function of
the object itself to do the actual work of the callback. This mechanism
allows me to make callbacks into C++ objects, using a "generic" C-style API,
which has no knowledge of my C++ class types.

There are obviously other cases where static_cast is used, but these are my
most common uses of it.

-Howard


 
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
reading binary file into memory. Converting from char to uint32,float, double, ASCII strings etc (static_cast< > ?) someone C++ 37 10-18-2011 01:32 AM
Re: PIL (etc etc etc) on OS X Kevin Walzer Python 4 08-13-2008 08:27 AM
Safely casting pointer types, purpose of static_cast, etc. jason.cipriani@gmail.com C++ 5 06-08-2008 07:26 PM
Is static_cast<int>(static_cast<double>(a)) == a? Bo Peng C++ 11 10-20-2006 12:59 PM
static_cast confusion Chandra Shekhar Kumar C++ 7 06-27-2003 02:54 PM



Advertisments