Item 28. Meaning of Pointer Comparison
In C++, an object can have multiple, valid addresses, and pointer comparison is not a question about addresses. It’s a question about object identity.
class Shape { ... };class Subject { ... };class ObservedBlob : public Shape, public Subject { ... };
In this hierarchy, ObservedBlob
is derived from both Shape
and Subject
, and (because the derivation is public) there are predefined conversions from an ObservedBlob
to either of its base classes.
ObservedBlob *ob = new ObservedBlob;Shape *s = ob; // predefined conversionSubject *subj = ob; // predefined conversion
The availability of these conversions means that a pointer to an ObservedBlob
may be compared to a pointer to either of its base classes.
if( ob ...
Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.