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. 15 1690
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
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.
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
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
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
"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.
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.
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.
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
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
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.
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?
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
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
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |