Effective C + + clause 38 by composite molding out of has-a or "according to something to achieve"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.