?
Solved

When To Use static_cast as opposed to traditional methods

Posted on 1998-03-15
5
Medium Priority
?
391 Views
Last Modified: 2008-02-01
C++ introduced the use of new casting operators.    In a book I am reading I have came across
static_cast as a way to convert types to other types.  Previously I would  use the following method of
casting.

while ((!isspace(pBuffer[location])) && ((unsigned int)location < strlen(pBuffer)))

Should I abandon the above method of casting for the static_cast method used
below.

while ((!isspace(pBuffer[location])) && (static_cast<unsigned int>(location) < strlen(pBuffer)))

What are the advantages of using static_cast as opposed to direct casting as
in the first line of code?  Are there any advantages to using the former casting method I was used to.

Thanks For The Input.
Darrell
0
Comment
Question by:larockd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1183503
static_cast has basically the same power and meaning as the general-purpose c-style cast.  If you are programming in C++
you should abandon the old style C cast.  Also, the new style
cast in C++ is easier to indentify.
0
 

Author Comment

by:larockd
ID: 1183504
I want to know what the advantages are.  Why did they create static_cast and abandoned the old casting style.  What are the benefits.  

As in my question I posed these questions which were not answered in your proposed answer
What are the advantages of using static_cast as opposed to direct casting as
in the first line of code?  Are there any advantages to using the former casting method I was used to.
0
 
LVL 3

Accepted Solution

by:
q2guo earned 10 total points
ID: 1183505
ok, static_cast is exactly the same as the old c style cast.  It was introduced in C++ along with three other types of casts.  These three other types of casts are very different from the old style cast.  

So, in your example using static_cast didn't give you any advantage except it is easier to spot than the c style.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183506
As q2quop said, the new static cast is FUNCTIONALLY identical to the old cast.  That is, it produces the same result.  However there is an important difference.  It stands out (it is ugly as sin.)  This can makes sure that people reading the code don't miss it by mistake.  The old cast just consists of a type name and parenthesis and in a complex expression can easily be missed.

In most cases, I prefer the old cast myself, unless it looks like it is going to get burried.
0
 

Author Comment

by:larockd
ID: 1183507
Thanks For the input..

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question