473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

static_cast etc

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

Jul 23 '05 #1
3 1893
* sh********@gmail.com:

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?
Jul 23 '05 #2
<sh********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #3

<sh********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gary Labowitz | last post by:
Am I doing this correctly? It is a sample program for my class. #include <iostream> using namespace std; int main( ) { int x=3, y=4;
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
9
by: news.ir.com.au | last post by:
Hi, In the following code I get the compiler error: error C2243: 'static_cast' : conversion from 'class B *' to 'class A *' exists, but is inaccessible I understand why I get this error and...
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
2
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. ...
19
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the...
24
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
3
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.