Item 28. Meaning of Pointer Comparison
An object may have multiple valid addresses (the pointer to the pointer to the base class), and if there are multiple pokes to point to it, these pointers may vary. So when comparing these pointers, is the address of the pointer to the address of the object? Or is it compared to the indication of the pointer to the object?
E.g:
Class shape {...}; class subject {...}; class observedblob: public shape, public subject {...};
Observedblob * ob = New observedblob; SHAPE * S = OB; SUBJECT * SUBJ = OB;
IF (ob == s) ... if (Subj == OB) ...
In this case, both of the above IFs are TRUE, even if OB, S, SUBJ's address is different. Why is this so? Since OB, S, SUBJ points to the same object, the compiler must ensure that OB is the same (SUBJ) (S and SUBJ have no inheritance relationship, all of them must differ). To achieve this, the compiler makes a pointer in which a pointer is added (or subtract) a suitable offset Delta, but if both are NULL, then affirmation. What is the size of this offset? Only the compiler knows that it is saved in type information. Then OB == Subj is replaced with: OB? (OB Delta == Subj): (SUBJ == 0)
What is the type information of the pointer? That may be judged. Void * v = subj; if (ob == v) // not equal! Due to the Void * lost type information, the compiler has to compare the pure address. In this way, the correctness of the correctness of the pointer to the key object is large.