C++: reinterpret cast

A

A

Hi,

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


Regards,
A
 
C

Colin

A said:
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.
 
O

.oO LGV Oo.

A said:
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...
 
K

Karl Heinz Buchegger

.oO LGV Oo. said:
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>
 
O

.oO LGV Oo.

Karl Heinz Buchegger said:
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...
 
J

Jonathan Hoyle

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top