In my IsSame
function I would like to return true
if both pointers are pointing to objects of the same type. So only the middle call should return true
. D1
and B
shouldn't be considered the same.
The below seems to be exactly what I want but is it safe according to the standard?
#include <stdio.h>
class B { virtual void foo() {} };
class D1 : public B { };
class D2 : public B { };
class D3 : public B { };
bool IsSame(B*a, B*b) {
if (a == 0 || b == 0)
return false;
return *(intptr_t*)a == *(intptr_t*)b;
}
int main() {
D1 d1;
D2 d2;
D1 d1b;
B b;
printf("%d %d %d\n", IsSame(&d1, &d2), IsSame(&d1, &d1b), IsSame(&d1, &b));
}
Output:
0 1 0