By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

C++: reinterpret cast

P: n/a
A
Hi,

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

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a

"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

P: n/a


".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

P: n/a

"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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.