473,586 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between static_cast and reinterpret_cas t

I never used reinterpret_cas t -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Can I assume that reinterpret_cas t is not safe and should not be used?
Does it always succeed even if the cast is garbage?

Aug 8 '06 #1
7 10890
Peter wrote:
I never used reinterpret_cas t -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Get a newer book. ARM is good, but Acc'd C++ or TC++PL are probably
more up to date on many things...

'reinterpret_ca st' is used to convert pointers to objects to integral
values (and back), if there is a type that can hold the entire value;
between pointers of different functions; between pointers and references
of unrelated object types.
Can I assume that reinterpret_cas t is not safe and should not be used?
No. It should be used when the requirements call for it.
Does it always succeed even if the cast is garbage?
No.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2

Victor Bazarov wrote:
Does it always succeed even if the cast is garbage?

No.
so reinterpret_cas t can fail. During runtime or during compile time?

Aug 8 '06 #3
Peter posted:
I never used reinterpret_cas t -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Can I assume that reinterpret_cas t is not safe and should not be used?
Does it always succeed even if the cast is garbage?

I use reinterpret_cas t where I can't use static_cast.

--

Frederick Gotham
Aug 8 '06 #4
Frederick Gotham wrote:
Peter posted:
I never used reinterpret_cas t -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Can I assume that reinterpret_cas t is not safe and should not be used?
Does it always succeed even if the cast is garbage?


I use reinterpret_cas t where I can't use static_cast.
Sorry to hear that.

(1) Sometime when static_cast doesn't work, it's because you have a
const issue - you should use a const_cast (rather than a
reinterpret_cas t) or figure out why you have a const problem.

(2) The strategy of "If this hammer (static_cast) doesn't work, I'll
just get myself a bigger hammer" is generally a bad one. Unless you're
doing the kind of low-level casts Victor B. described, reinterpret_cas t
should not be necessary.

Best regards,

Tom

Aug 8 '06 #5
Peter wrote:
Victor Bazarov wrote:
>>Does it always succeed even if the cast is garbage?

No.

so reinterpret_cas t can fail. During runtime or during compile time?
It should only "fail" (be flagged as inappropriate) during compile time.
The run-time stuff is usually simply undefined behaviour. You cannot
detect it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #6
Thomas Tutone wrote:
>
(1) Sometime when static_cast doesn't work, it's because you have a
const issue - you should use a const_cast (rather than a
reinterpret_cas t) or figure out why you have a const problem.
A reinterpret_cas t won't bash const either. You have to use
(preferably) a const_cast OR a C-style cast.
]
Aug 9 '06 #7

Thomas Tutone wrote:
Frederick Gotham wrote:
Peter posted:
I never used reinterpret_cas t -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help.
Can I assume that reinterpret_cas t is not safe and should not be used?
Does it always succeed even if the cast is garbage?
The ARM is from 1989. It has been outdated for quite some time. For a
standard, see the ISO standard (link to a draft and explanation of how
to get the final version on my C++ page). For explanations, see D&E or
TC++PL3 (or any other complete modern C++ book).

The "new style casts" postdate the ARM. "reinterpret_ca st" is the least
constrained.new cast. You use it if/when you really need to break the
type system. In goodc code, that's rare.

-- Bjarne Stroustrup; http://www.research.att.com/~bs

Aug 9 '06 #8

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

Similar topics

7
7366
by: buds | last post by:
Hi all, Following is the assigment operator of a derived class Derived& Derived::operator=(const Derived& inDerived) { //to assign to the base class object the following statement static_cast<Base&>(*this)=inDerived;//works fine
11
5134
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an...
26
2527
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void...
3
5987
by: Kobe | last post by:
Hi, if I need to convert a size_t to an int, in "older" C++ I'd write the following code (using C-like "casting"): <CODE> std::vector<...> v; int count = (int) v.size(); // v.size() returns size_t </CODE>
19
3712
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the other.
24
4731
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
2
2656
by: Lucy Ludmiller | last post by:
I have two classes: class base { ... }; and class derived : public base { ... }; I have a function :
1
7109
by: =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= | last post by:
In C++ we can not overload the explicit typecast operator like static_cast, dynamic_cast, const_cast and reinterpret_cast. The only we can do is the implicit typecast operator like operator MyClass::MyDistType(). I agree there are maybe no meaning to overload dynamic_cast/const_cast/ reinterpret_cast, except the static_cast. If we enable...
2
4287
by: zeeshan708 | last post by:
what is the difference between a reinterpret_cast and static_cast,,both do casting at compile time so whats the difference????
0
7915
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...
0
7841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
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...
0
5392
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...
0
3838
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...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1184
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...

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.