reinterpret_cast is not to the same as C cast. In case of the
pointer to pointer conversion between any pointers, it should be
static_cast or dynamic_cast. In the case of between totally non related
types, like an integer to a pointer, it should be reinterpret_cast. But
this is quite rare case. You can do like
SpecialType * pFixedIODevice = reinterpret_cast(0x1234);
As this sample program shows,
reinterpret_cast preserves the bit pattern. The story about
casting integral type may differ, but the basic idea --- do not
change the bit pattern --- stays for reinterpret_cast.
The object memory arrangement is usually implementation dependent.
Derived * == test0() <= Base0*: 0x??????
test1() <= Base1*: 0x?????? + 04 (in case of sizeof(pointer) = 4)
The most likely crashing case is
- (1) the object is newed as Derived*
- (2) p = static_cast(Derived*)
- (3) q = reinterpret_cast(p)
- (4) q->test1()
(1)Derived * == test0() + 00
test1() + 04 <= (2) p, (3) reinterpret_cast<Derived*)>(p)
+ 08 <= (4) wrong test1() ... may crash if you are lucky.
This test is based on Stefan R.'s code. 2007.6.13