473,387 Members | 3,820 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,387 software developers and data experts.

casting question

Can someone explain to me the difference between static_cast<T>(v) and
(what's the term for it?) old-style C cast?

UINT value1 = 0;

int value2 = (int)value1;
int value2 = static_cast<int>(value1);

TIA,
Sharon

May 25 '06 #1
7 1092
dotnetchic wrote:
Can someone explain to me the difference between static_cast<T>(v) and
(what's the term for it?) old-style C cast?

UINT value1 = 0;

int value2 = (int)value1;
int value2 = static_cast<int>(value1);


static_cast is more explicit and more restrictive - you know on sight that a
static_cast does not:
- Cast away constness
- Cast between unrelated pointer types
- Reinterpret the bits of one type as another type

The C-style cast, however, is a chameleon - depending on the context, the
C-style (or function-style) cast is equivalent to either static_cast,
reinterpret_cast, or one of those two followed by const_cast.

So, in your example, there's no difference at all - the C-style cast in that
context is exactly the same as a static_cast. Use of the C++ casts is
prefereable because of the improved readability of the code, but there's no
functional (i.e. runtime or code generation) difference.

-cd
May 26 '06 #2
Thanks. Just what I was looking for.

I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?

I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.

Sharon

May 26 '06 #3
dotnetchic wrote:
Thanks. Just what I was looking for.

I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?
Think about this - how would you search for c-style casts using your favorit
text editor? Of course, finding (int) is easy, but in general you can
declare a type in a cast expression, so what comes between the () can be
arbitrarily long and ugly. Finding static_cast< is always easy.

I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.


You'lll grow to love them in time!

-cd
May 26 '06 #4
>I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?


Clearly it's not ;)

But you can search for the new style casts easier!

Dave
May 26 '06 #5
> Of course, finding (int) is easy, but in general you can
declare a type in a cast expression, so what comes between the () can be
arbitrarily long and ugly.


very true. I'm porting code right now that should go in the coding
horror hall of fame.
I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.


You'lll grow to love them in time!


I'll take your word on that.

Cheers!
Sharon

May 26 '06 #6

"dotnetchic" <do********@gmail.com> skrev i meddelandet
news:11**********************@j73g2000cwa.googlegr oups.com...
Thanks. Just what I was looking for.

I'm with you on the first part. The C++ cast is preferrable because
the function-style cast evaluates one of several other things. But
how, exactly, is
static_cast<int>(value)
more readable than
(int)(value)
?

I suppose I just have a bad taste for C++ with all its ugly angle
brackets and underscores :) Thanks for the info.


They were made ugly and highly visible on purpose. You are supposed to
avoid them as much as possible!

When you have a code walkthru with your collegues, there is no chance
for them to miss asking you about this piece of code. "Why do you
think a cast is the best way to solve this problem?". Have the answer
ready!
Bo Persson
May 26 '06 #7
While I appreciate the advice, I'm referring to a large code base which
I am porting/maintaining; not that which I wrote. You are right,
casting should be avoided as much as possible, and I plan to do
something about that during the restructure of this project.

Regards,
Sharon

May 26 '06 #8

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

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.