469,612 Members | 1,529 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Quick cast style question

If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
15 1526
On Fri, 30 Apr 2004 21:05:43 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;
Personally, I'd use the reinterpret_cast, since the compiler will diagnose
attempts to use it in a situation where one of the other new-style casts
would be the right one. So the former is saying "do the cast if in fact we
are truly requiring reinterpret, and the others wouldn't do, else tell me
I've screwed up", while the latter says "just shut up and do it."
-leor
?


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Christopher Benson-Manica wrote:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

?

Somehow I am always sceptic with the usage of reinterpret_cast here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cast is dangerous and would probably tie you to a specific
implementation.

--
Karthik
Humans please 'removeme_' for my real email.
Jul 22 '05 #3
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c6**********@chessie.cirr.com:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;


C-style casts were provided for compatibility with old code. In C++ you
should only use the 'new'-style casts, as they show your intents clear in
the code. 'reinterpret_cast' is there to show that something drastic is
going on.

Code editors should highlight it in bloody red... or even flashing bloody
red.

Cheers.
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #4
Karthik <re*******************@yahoo.com> wrote in news:4092c3bc$1
@darkstar:
Christopher Benson-Manica wrote:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

?

Somehow I am always sceptic with the usage of reinterpret_cast here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cast is dangerous and would probably tie you to a specific
implementation.


'reinterpret_cast' is the only option in that situation. The compiler
would refuse to compile the above code when 'static_cast' was used.
'dynamic_cast', on the other hand, only works for casting
pointers/references within an inheritance hierarchy.

Cheers.
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #5
On Fri, 30 Apr 2004 14:20:39 -0700, Karthik
<re*******************@yahoo.com> wrote:
Christopher Benson-Manica wrote:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

?

Somehow I am always sceptic with the usage of reinterpret_cast here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cast is dangerous and would probably tie you to a specific
implementation.


You may want to try the different forms and examine the results. In fact,
reinterpret_cast was the correct choice here (of the different casts. I
can't speak to the appropriateness here of using a cast, versus not using
one, in the first place...)
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #6

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c6**********@chessie.cirr.com...
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;


Prefer the former - it was added to C++ as an improvement over the latter.
Jul 22 '05 #7
Leor Zolman <le**@bdsoft.com> spoke thus:
You may want to try the different forms and examine the results. In fact,
reinterpret_cast was the correct choice here (of the different casts. I
can't speak to the appropriateness here of using a cast, versus not using
one, in the first place...)


Well, the design decision that makes a cast necessary in the first
place is probably of debatable merit. Given that, the cast is pretty
much required...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #8


bartek wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c6**********@chessie.cirr.com:

If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

C-style casts were provided for compatibility with old code. In C++ you
should only use the 'new'-style casts, as they show your intents clear in
the code. 'reinterpret_cast' is there to show that something drastic is
going on.

Code editors should highlight it in bloody red... or even flashing bloody
red.


I agree when casting classes, but I still like the old style casts when
doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.
Jul 22 '05 #9
On Sat, 01 May 2004 21:07:13 GMT, Jake Montgomery
<ja******************@yahoo.com> wrote:
I agree when casting classes, but I still like the old style casts when
doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.
Well, if you don't like the new_style cast for aesthetic reasons, or if you
specifically don't want to be warned when the cast isn't doing what you
think it is supposed to do (a benefit of using new-style casts), then
consider this: You have a large app and you wish to locate all the places
you've performed a cast. Tell us how to do it, for certain, with your
old-style casting used all over the place, vs. new-style casts.

Note: assume the coders have been very sloppy with style, writing (type), (
type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.

Note #2: Hope you have a tool supporting good regular expression syntax if
you want to be exhaustive with this ;-)
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #10
Jake Montgomery <ja******************@yahoo.com> wrote in
news:40**************@yahoo.com:

(...)
I agree when casting classes, but I still like the old style casts
when doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.


Actually in similar cases, where automatic conversion is possible, I'd
suggest using the 'fake' constructor syntax:

double d1 = double(n1) / double(n2);

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #11

Leor Zolman wrote:
On Sat, 01 May 2004 21:07:13 GMT, Jake Montgomery
<ja******************@yahoo.com> wrote:

I agree when casting classes, but I still like the old style casts when
doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.

Well, if you don't like the new_style cast for aesthetic reasons, or if you
specifically don't want to be warned when the cast isn't doing what you
think it is supposed to do (a benefit of using new-style casts), then
consider this: You have a large app and you wish to locate all the places
you've performed a cast. Tell us how to do it, for certain, with your
old-style casting used all over the place, vs. new-style casts.

Note: assume the coders have been very sloppy with style, writing (type), (
type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.


For simple type conversions (which is what my post spoke of) your
task seems extremely contrived. I can never imagine being concerned
with every occurrence of a int to double cast in the entire app. Could
you come up with a real world situation?
Note #2: Hope you have a tool supporting good regular expression syntax if
you want to be exhaustive with this ;-)
-leor

Yes I do - I use the awesome, but soon-to-be-defunct-due-to-buyout
CodeWright. In the extremely unlikely case that I needed to, I could
indeed do a multiple file grep that would find every occurrence of (say)
a cast to double.
Jul 22 '05 #12
bartek wrote:
Jake Montgomery <ja******************@yahoo.com> wrote in
news:40**************@yahoo.com:

(...)

I agree when casting classes, but I still like the old style casts
when doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.

Actually in similar cases, where automatic conversion is possible, I'd
suggest using the 'fake' constructor syntax:

double d1 = double(n1) / double(n2);


I have no problem with this syntax ... just habit, and lack of a good
reason to change, keep me using the (double)n1 syntax. I will admit,
that the 'fake' constructor syntax does make things a bit clearer. Are
there any differences between the two in terms of generated code?


Jul 22 '05 #13
On Tue, 04 May 2004 00:09:29 GMT, Jake Montgomery
<ja******************@yahoo.com> wrote:

Well, if you don't like the new_style cast for aesthetic reasons, or if you
specifically don't want to be warned when the cast isn't doing what you
think it is supposed to do (a benefit of using new-style casts), then
consider this: You have a large app and you wish to locate all the places
you've performed a cast. Tell us how to do it, for certain, with your
old-style casting used all over the place, vs. new-style casts.

Note: assume the coders have been very sloppy with style, writing (type), (
type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.


For simple type conversions (which is what my post spoke of) your
task seems extremely contrived. I can never imagine being concerned
with every occurrence of a int to double cast in the entire app. Could
you come up with a real world situation?


In the general case, given potentially large-scale projects (code reviews,
etc), the question of how often casts are used /is/ a real-world situation.
If in your project, you know that the only way you're using casts is the
one way you're using them, then you might not personally have this issue.

(And the "task" in question was rather rhetorical, just to point out the
value of the unique keywords used for new-style casts.)
Note #2: Hope you have a tool supporting good regular expression syntax if
you want to be exhaustive with this ;-)
-leor

Yes I do - I use the awesome, but soon-to-be-defunct-due-to-buyout
CodeWright. In the extremely unlikely case that I needed to, I could
indeed do a multiple file grep that would find every occurrence of (say)
a cast to double.


Yes, but again, not everyone who might at some point find themselves
needing to locate all their cast operations would necessarily have the
right tool for the job, as well as the requisite skill with r.e.'s, at
their fingertips.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #14
Jake Montgomery <ja******************@yahoo.com> wrote in
news:40**************@yahoo.com:
bartek wrote:
Jake Montgomery <ja******************@yahoo.com> wrote in
news:40**************@yahoo.com:

(...)

I agree when casting classes, but I still like the old style casts
when doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.

Actually in similar cases, where automatic conversion is possible, I'd
suggest using the 'fake' constructor syntax:

double d1 = double(n1) / double(n2);


I have no problem with this syntax ... just habit, and lack of a good
reason to change, keep me using the (double)n1 syntax. I will admit,
that the 'fake' constructor syntax does make things a bit clearer. Are
there any differences between the two in terms of generated code?


The compiler I'm using generates exactly the same code in either case.
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #15


Leor Zolman wrote:
On Tue, 04 May 2004 00:09:29 GMT, Jake Montgomery
<ja******************@yahoo.com> wrote:

Well, if you don't like the new_style cast for aesthetic reasons, or if you
specifically don't want to be warned when the cast isn't doing what you
think it is supposed to do (a benefit of using new-style casts), then
consider this: You have a large app and you wish to locate all the places
you've performed a cast. Tell us how to do it, for certain, with your
old-style casting used all over the place, vs. new-style casts.

Note: assume the coders have been very sloppy with style, writing (type), (
type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.


For simple type conversions (which is what my post spoke of) your
task seems extremely contrived. I can never imagine being concerned
with every occurrence of a int to double cast in the entire app. Could
you come up with a real world situation?

In the general case, given potentially large-scale projects (code reviews,
etc), the question of how often casts are used /is/ a real-world situation.
If in your project, you know that the only way you're using casts is the
one way you're using them, then you might not personally have this issue.

(And the "task" in question was rather rhetorical, just to point out the
value of the unique keywords used for new-style casts.)


First off, I just want to re-iterate that I like, advocate for, and use
new_style casts for all situations except the 'trivial' type change
situations I mentioned earlier. That said, please excuse my ignorance
.... my experience has been mostly with (large) applications, but I
really dont see where finding all cats in a large project is a _common_
real world problem.

I would be (honestly) appreciative and enlightened if you would give
me an example of how it would be useful to search on _all_ (non-dynamic)
casts in a large project. (something not too contrived.)

I would be surprised (honestly), but appreciative, if you could give
me a not-overly-contrived example of needing to find all simple numeric
type casts (like those I am 'advocating') in a large project.

Thanx.

[Snip]
Jul 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.