473,386 Members | 1,775 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,386 software developers and data experts.

difference between static_cast and reinterpret_cast

I never used reinterpret_cast -- 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_cast is not safe and should not be used?
Does it always succeed even if the cast is garbage?

Aug 8 '06 #1
7 10873
Peter wrote:
I never used reinterpret_cast -- 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_cast' 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_cast 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_cast can fail. During runtime or during compile time?

Aug 8 '06 #3
Peter posted:
I never used reinterpret_cast -- 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_cast is not safe and should not be used?
Does it always succeed even if the cast is garbage?

I use reinterpret_cast where I can't use static_cast.

--

Frederick Gotham
Aug 8 '06 #4
Frederick Gotham wrote:
Peter posted:
I never used reinterpret_cast -- 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_cast is not safe and should not be used?
Does it always succeed even if the cast is garbage?


I use reinterpret_cast 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_cast) 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_cast
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_cast 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_cast) or figure out why you have a const problem.
A reinterpret_cast 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_cast -- 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_cast 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_cast" 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
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 ...
11
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...
26
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...
3
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...
19
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...
24
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
by: Lucy Ludmiller | last post by:
I have two classes: class base { ... }; and class derived : public base { ... }; I have a function :
1
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...
2
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
0
BarryA
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...
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:
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...
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.