I have two pointers to objects and I want to test if they are the exact same object in the most robust manner. I explicitly do not want to invoke any operator ==
overloads and I want it to work no matter what base classes, virtual base classes and multiple inheritance is used.
My current code is this:
((void*)a) == ((void*)b)
And for my case this works. However, that doesn’t work for this case:
class B1 {};
class B2 {};
class C : public B1, public B2 {}
C c;
B1 *a = &c;
B2 *b = &c;
Subbing in reinterpert_cast
, static_cast
or dynamic_cast
doesn't work either.
Particularly I'm hoping for something that ends up really simple and efficient. Ideally it wouldn't require any branch instructions to implement and would do something like, adjust the pointer to the start of the object and compare.