472,353 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 1838
* 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...
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...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.