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

reinterpret_cast or static_cast ?

I have two classes:

class base { ... };

and

class derived : public base { ... };
I have a function :

void foo(const base*) { ... }

I want to be able to cast back to derived (I only have a ptr to base in
this compilation unit), and then invoke a method on object derived

- do I use a static_cast or reinterpret_cast to cast "upward" ?
- any reason why one is a better choice than the other ?

Sep 3 '06 #1
2 2644
* Lucy Ludmiller:
I have two classes:

class base { ... };

and

class derived : public base { ... };
I have a function :

void foo(const base*) { ... }
If a nullvalue for the pointer is not meaningful, make that

void foo( base const& )

I want to be able to cast back to derived (I only have a ptr to base in
this compilation unit), and then invoke a method on object derived
Then make that

void foo( derived const& )

- do I use a static_cast or reinterpret_cast to cast "upward" ?
static_cast or dynamic_cast.

If base has one or more virtual member functions, make that
dynamic_cast. Choose casting of reference for exception, or casting of
pointer + assert for assertion. Be sure to test that piece of code
extremely thoroughly.

Or (much!) better, design the cast away.

- any reason why one is a better choice than the other ?
They're different. Conceptually a reinterpret_cast reinterprets the
bits of the pointer value as some other pointer type, without doing
necessary adjustments. In this context reinterpret_cast yields
undefined behavior.

Generally (there is one exception) you can only use the result of a
reinterpret_cast in a portable way by casting back to the original type.

For someone who is unsure of casting, reinterpret_cast means you have a
bug, and any cast whatsoever means you have a potential set of bugs.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 3 '06 #2

Alf P. Steinbach wrote:
If base has one or more virtual member functions, make that
dynamic_cast. Choose casting of reference for exception, or casting of
pointer + assert for assertion. Be sure to test that piece of code
extremely thoroughly.
or boost::polymorphic_downcast

Sep 7 '06 #3

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

Similar topics

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...
15
by: Aman | last post by:
Hi, wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3 trying to see the byte order of an int or short int by converting to char* . doesn't work . the char* cpt doesn't seem to be...
5
by: Christopher Benson-Manica | last post by:
enum foo {a,b,c,d}; int main() { unsigned int bar=reinterpret_cast< unsigned int >( a ); return bar; } g++ rejects the code based on the reinterpret_cast, so I presume it is not legal. ...
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...
7
by: Peter | last post by:
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....
3
by: Nate Barney | last post by:
I have: // base class for Vector and Matrix template <unsigned N,typename Value=float> class NonScalar { public: Value *ptr() { return e; } const Value *ptr() const { return e; }
27
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
3
by: J.M. | last post by:
I have data in a double array of length 2N, which actually represents complex numbers with real and imaginary parts interlaced. In other words, elements in this array with even indices represents...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.