473,507 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++: reinterpret cast

A
Hi,

I have not been able to find an example for using the reinterpret cast. Any
ideas?
Regards,
A

Jul 22 '05 #1
5 9709
"A" <A@iprimus.com.au> writes:
Hi,

I have not been able to find an example for using the reinterpret cast. Any
ideas?


I don't think you'll find very many of them because it's such a bad
idea to use them in the first place.

--
These largely uninformed opinions are not those of my employer
in any way, shape, or form.
Jul 22 '05 #2

"A" <A@iprimus.com.au> a écrit dans le message de
news:3f********@news.iprimus.com.au...
Hi,

I have not been able to find an example for using the reinterpret cast. Any ideas?
Regards,
A


here's a small and very simple example of up/down-casting :

class A
{
};

class B : public A
{
};

where an A is awaited, you can pass a B since B is a specialisation; ex :

void fct(A *)
{
}

B *pB = new B();
fct(pB);

on the other, where a B is expected, you *may* pass a A, with the use of the
reinterpret_cast :

void fct2(B *);

A *pA = new A();
fct2(pA); // compiler error, polymorphism does not apply with downcasting
fct(reinterpret_cast<B *>(pA)); // force the compiler to do what you want

of course, with such things, you'd better be sure or what you're doing... ie
if the code of fct2 uses an attribute of B that does not exist in A, you may
get some undefined behaviour (?) or maybe crash (?) (I let those who know
the standard by heart clarify this point...)

btw, you can always use reinterpret_cast for casting any pointers ; ie
you're dealing with a 32 bits integers array, and you want to change a
byte... here's come a reinterpret_cast + offset...
Jul 22 '05 #3


".oO LGV Oo." wrote:

on the other, where a B is expected, you *may* pass a A, with the use of the
reinterpret_cast :

void fct2(B *);

A *pA = new A();
fct2(pA); // compiler error, polymorphism does not apply with downcasting
fct(reinterpret_cast<B *>(pA)); // force the compiler to do what you want


On the other hand you can also do:

double* pC;
fct( reinterpret_cast< B* >( pC ) );

:-)

Here is what Microsoft has to say about reinterpret_cast

<Quote>
The reinterpret_cast operator allows any pointer to be converted into any other pointer
type. It also allows any integral type to be converted into any pointer type and vice
versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired
conversion is inherently low-level, you should use one of the other cast operators.

The reinterpret_cast operator can be used for conversions such as char* to int*, or
One_class* to Unrelated_class*, which are inherently unsafe.

The result of a reinterpret_cast cannot safely be used for anything other than being
cast back to its original type. Other uses are, at best, nonportable.
</Quote>

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

"Karl Heinz Buchegger" <kb******@gascad.at> a écrit dans le message de
news:3F***************@gascad.at...


".oO LGV Oo." wrote:

on the other, where a B is expected, you *may* pass a A, with the use of the reinterpret_cast :

void fct2(B *);

A *pA = new A();
fct2(pA); // compiler error, polymorphism does not apply with downcasting fct(reinterpret_cast<B *>(pA)); // force the compiler to do what you want


On the other hand you can also do:

double* pC;
fct( reinterpret_cast< B* >( pC ) );

:-)


yep, in fact we can do almost what we want with the reinterpret_cast :) and
THIS IS the trap.... I just wanted to show a common use of this kinda cast
for downcasting ; anyway, one should be careful and aware of what's going
on...
Jul 22 '05 #5
> I have not been able to find an example for using the reinterpret cast. Any
ideas?


The best one I can think of involves using a predefined API which you
wish to use a long integer parameter to hold a pointer. For example,
Photoshop's main entry point into their DLL's have a long that is
expected to be a pointer holder for you. Why not just call it a void
*? Well, in the ugly old days of Windows 3.1, pointers were 16-bits,
whereas on the Mac they've always been 32-bit. So that parameter
sizes remain cross-platform, Adobe used an integer value, which
reinterp_cast'ing was meant for.

Jonathan Hoyle
Gene Codes Corporation
Jul 22 '05 #6

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

Similar topics

2
2101
by: Frank A. Uepping | last post by:
Hi, look at this function, which does some low-level IO. void writen(int fd, const void* data, size_t size) { while (size) { int n = write(fd, data, size); if (n < 0) throw logic_error...
5
1903
by: Dave Townsend | last post by:
Hi, I'm trying to port a piece of code from Windows to Linux, the following segment illustrates the coding problemI have: Under Linux, the reinterpret_cast line doesn't compile, the compiler...
0
3573
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
17
2660
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
3396
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
15
6943
by: thomas.mertes | last post by:
For a hash function I want to reinterpret the bits of a float expression as unsigned long. The normal cast (unsigned long) float_expression truncates the float to an (unsigned long) integer. ...
5
2344
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
1
2034
by: Muhammad Haseeb | last post by:
plz help me how to use the reinterpret cast operator in filing in c++
0
1322
by: NiveditaB06 | last post by:
it is recommended not to use reinterpret cast that is mostly used for type converting one pointer type to other totally different type( like int ).After casting one member variable to another type...
0
7223
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,...
0
7114
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
7377
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...
0
5623
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,...
1
5045
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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...

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.