473,657 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_c ast<const myClass*>(my_va l);

or

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

?

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

const myClass* a=reinterpret_c ast<const myClass*>(my_va l);

or

const myClass* a=(const myClass*)myVal;
Personally, I'd use the reinterpret_cas t, 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_c ast<const myClass*>(my_va l);

or

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

?

Somehow I am always sceptic with the usage of reinterpret_cas t here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cas t 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.c yberspace.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_c ast<const myClass*>(my_va l);

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_ca st' 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.c om> 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_c ast<const myClass*>(my_va l);

or

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

?

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


'reinterpret_ca st' 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.c om> 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_c ast<const myClass*>(my_va l);

or

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

?

Somehow I am always sceptic with the usage of reinterpret_cas t here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_ca st 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_cas t 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

"Christophe r Benson-Manica" <at***@nospam.c yberspace.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_c ast<const myClass*>(my_va l);

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.co m> spoke thus:
You may want to try the different forms and examine the results. In fact,
reinterpret_cas t 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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #8


bartek wrote:
Christopher Benson-Manica <at***@nospam.c yberspace.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_c ast<const myClass*>(my_va l);

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_ca st' 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.co m> 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

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

Similar topics

0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.