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