Casting void*, comparing void*
Given the following example code:
class A {
public: A() {}
};
class B {
public; B() {}
};
A* a = new A();
B* b = new B();
void *aVoid = a;
void *bVoid = b;
bool compareVoid = (aVoid == bVoid); // (1) false?
B* aCastedToB = static_cast<B*> aVoid;
bool compareB = (aCastedToB == b); // (2) false or unspecified?
Unfortunately, I only have access to ISO/IEC 14882:2003 here. Maybe
someone can confirm whether my interpretation of the standard held at
that time, and, more importantly, whether it still holds for the current
version of the standard.
Re (2): Section 5.2.9 [expr.static.cast], ?? 10 says that a pointer to
cv1 void can be cast into an arbitrary pointer to cv2 T (with cv2 >=
cv1). Converting a pointer to void* and back to the original type will
yield the original value. Do I understand correctly that the standard
leaves open the result (and maybe also the behaviour?!) when the target
type of the cast is _not_ the original type? In particular: Is it
possible to make any statement about the result of equal-comparing a
pointer obtained in such a way with another pointer that "really" is of
that type?
Re (1): Regarding pointer comparisons, Section 5.9 [expr.eq] says: "Two
pointers of the same type compare equal if and only if they are both
null, both point to the same function, or both represent the same
address (3.9.2). This guarantees that the comparison in the example code
will yield false, right?
Best regards,
Jens
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]