Solved

When To Use static_cast as opposed to traditional methods

Posted on 1998-03-15
5
376 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
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks For the input..

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now