Solved

When To Use static_cast as opposed to traditional methods

Posted on 1998-03-15
5
387 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

Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

732 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