1. Public inheritance embodies the is-a relationship, requires the complete inheritance of the interface, and the composite embodiment has-a or "according to something to achieve" relationship.
When compounding occurs between objects in the application domain (something in the world, such as people, cars, a clip, video footage, etc.), the has-a relationship is displayed when it occurs within the implementation domain (artifacts in detail, such as buffers, mutexes, find trees, etc.), Showing the relationship of is-implementation-in-terms-of (according to something).
2. For is-implementation-in-terms-of, such as list-based implementation of set (which is possible when space is more important than time), it is obviously inappropriate to use public inheritance, because the set of actions that list can perform may not be allowed ( For example, you have two identical elements), so you can use a composite:
Template<set>classset{ Public: BOOLMemberConstt& Item)Const; voidInsertConstt&item); voidRemoveConstt&item); std::size_t size ()Const; ...Private: Std::list<T>Rep;}//specific implementation of a slightly
View Code
In the example above, the set object is "implemented according to list".
Effective C + + clause 38 by composite molding out of has-a or "according to something to achieve"