473,385 Members | 1,983 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,385 software developers and data experts.

When static_cast should be used?

Hi,

An example usage of static_cast is at
http://publib.boulder.ibm.com/infoce...tatic_cast.htm

But I don't understand why it is necessary to use
float d = static_cast<float>(j)/v;

Isn't
float d = float(j)/v;
enough?

I couldn't find the detailed discussion on those *_cast? Would you
please tell me some good online resources? Thanks!

Best wishes,
Peng

Jul 23 '05 #1
1 1902

<Pe*******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi,

An example usage of static_cast is at
http://publib.boulder.ibm.com/infoce...tatic_cast.htm

But I don't understand why it is necessary to use
float d = static_cast<float>(j)/v;

Isn't
float d = float(j)/v;
enough?

It isn't strictly necessary. float d = float(j)/v works. But there are
several reasons why you should use the various *_cast operators
1) It makes clear exactly what you are doing. static_cast forces
implicit conversions (int to float for instance) which is why you use it in
your example. const_cast casts away constness. reinterpret_cast makes the
computer pretend that a variable of one type is actually of another type.
dynamic_cast is used for run-time type identification; it checks whether a
pointer to a base type is actually a pointer to the target derived type then
returns the cast pointer if it is or 0 if it is not. A plain C cast can
replace any of these except dynamic_cast, but it is helpful to you and
anyone who will have to maintain your code later to be specific about what
you are doing.

2) It is much easier, both for humans and computers, to search for
static_cast in code than to look through all the parentheses to try to
determine which are casts.

Joe Gottman
Jul 23 '05 #2

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

Similar topics

4
by: Wenjie | last post by:
Hello, I have a class B to mediate/isolate between implementation and their users (like used in an abstract factory class pattern): // B.h class B { static B* getImpl(); static void...
2
by: Frank A. Uepping | last post by:
Hi, look at this function, which does some low-level IO. void writen(int fd, const void* data, size_t size) { while (size) { int n = write(fd, data, size); if (n < 0) throw logic_error...
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...
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...
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
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...
5
by: Mr. Ken | last post by:
I am calculating the phase of an IQ signal, which are polluted by AWGN gaussian noise. Thus, near pi/2, direct division of atan(Q/I) may yield outputs either +pi/2 or -pi/2. How do I handle this...
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; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.