Item 27. Capability Queries

Most times when an object shows up for work, it’s capable of performing as required, because its capabilities are advertised explicitly in its interface. In these cases, we don’t ask the object if it can do the job; we just tell it to get to work:


Even though we don’t know precisely what type of shape we’re dealing with, we know that it is-a Shape and, therefore, can draw itself. This is a simple and efficient—and therefore desirable—state of affairs.

However, life is not always that straightforward. Sometimes an object shows up for work whose capabilities are not obvious. For example, we may have a need for a shape ...

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.