For example, I have some class hierarchy (possibly, with all kinds of inheritance – public, private, public virtual, multi-inheritance, etc.):
class A {
int a;
public:
virtual ~A() {}
};
class B: public A { int b; };
class C: public virtual B { int c; };
class E: public virtual B { int e; };
class F: public C, public E { int f; };
Using casts I get pointers to every sub-object of the main “big” object:
F * f = new F;
E * e = f;
C * c = f;
B * b = f;
A * a = f;
What pairs of these pointers may I compare for equality (operator==) and why?
Will the comparison use delta-logic or some other technique?
What are the possible situations, when I can’t compare pointers to the same complex object?
What kind of object it can be?
I expect, that all of the pointers to the same object are always equal.
You can compare two pointers if one pointer type is implicitly convertible to the other; that is, if they both point to the same type, or one points to a base class of the other’s. The conversion will make the necessary adjustment to the address so that, if both pointers point to the same object, they will compare equal.
In this case, you can compare any pair except
c == e, since neither ofCnorEis derived from the other. To compare those, you would either need to cross-cast, or to convert both to their common base class; neither of these can be done implicitly.By the way, there’s no need for
dynamic_castin your code, since you’re casting to base class pointers and that safe conversion can be done implicitly.