static_cast etc

S

shrishjain

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
 
A

Alf P. Steinbach

* (e-mail address 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.
 
L

Larry Brasfield

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.
 
H

Howard

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top