Valid C ++: Clause 38: Use composite molding to produce has-a or "based on something"

Source: Internet
Author: User


Public inheritance is the relationship of "is-a", and the combination of "has-a" or "is implemented based on something (is-implemented-in-terms-) "meaning-when the combination occurs between objects in the application domain, it shows the has-a relationship; when it occurs in the implementation domain, it indicates the relationship" implemented based on something.

The application domain is equivalent to some things in the world you create, such as people and cars.

The latter object is the implementation of detailed manual products (this product is not available in the real world), such as what mutex, list, container and so on. These objects are the implementation areas of your software.


class Address{...};class PhoneNumber{...};class Person{      ...private:      std::string name_;      Address address_;      PhoneNumber voiceNumber_;      PhoneNumber faxNumber_;};


Instance: set structure. There is a set template in the standard library, which "Each element consumes three pointers" is achieved by using a balanced search tree, make sure that they have log (n) efficiency when searching, inserting, and deleting elements.

You may think of implementation like this:

Let set inherit stl: list:

Class Set: public list
{...}; // Apply list to set. Incorrect practice.
This is wrong! Because: The public inheritance is a-a relationship, and the parent class can do it, so can the subclass. But set is not a list, because listing is true. Some things are not true for the set object. For example, a list can contain duplicate elements. If 30 is inserted to the list Twice, the list contains two 30 s. If 30 is inserted to the set Twice, set contains only one 30.
Therefore, the two classes are not in the is-a relationship. It should not be public inheritance. The correct method is that the set object can be implemented based on a list object:

    class Set {public:bool member(const T& item) const;void insert(const T& item);void remove(const T& item);size_t size() const;private:list
      bool Set
       ::member(const T& item) const {return find(rep.begin(), rep.end(), item) != rep.end();}template 
        void Set
         ::insert(const T& item) {if(!member(item)) rep.push_back(item);}template 
          void Set
           ::remove(const T& item) {typename list
            ::iterator it = find(rep.begin(), rep.end(), item);if(it != rep.end()) rep.erase(it);}template 
             size_t Set
              ::size() const {return rep.size();}


(1) The meaning of composite is completely different from that of public inheritance.

(2) In the application domain, combination means has-a (with one ). In the implementation domain, combination means is-implemented-in-terms-of (implemented based on something ).

Related Article

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: 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.