[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

When To Use static_cast as opposed to traditional methods

Posted on 1998-03-15
5
Medium Priority
?
394 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

656 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