Effective C ++ 2e Item40

zhaozj2021-02-11  201

Terms 40: Through the layering, "has one" or "to achieve"

Make some objects of a class into data members of another class, so that a class is constructed on another class, which is called "layering". E.g:

Class address {...}; // Someone's residence

Class Phonnumber {...};

Class Person {public: ...

Private: string name; // The lower object address address; // 同 同 同 同 同 同 上 同 同 同};;

In this example, the Person class is considered to be the upper layer placed in String, Address, and Phonenumber classes because it contains those types of data. The term "layered" has a lot of synonyms, which are often referred to as: Composition, Containing or embedding.

Terms 35 explain the meaning of public inheritance is "Yes". Correspondingly, the hierarchical meaning is "there is a" or "use ... to be implemented".

The above Person class shows the relationship of "there is a". A Person object "has a" name, address, phone number, and fax number. You can't say, a person "is a" name or a person "is an" address; you have to say, "There is a" name, "has an" address, and so on. Most people have no difficulty in distinguishing these, so confused "Is a" and "there is a" situation is relatively rare.

Slightly a bit trouble is distinguishing "is a" and "implementation with ...". For example, suppose a class template is required to indicate a collection of any object, and no elements are repeated in the collection. In programming, reuse is a better thing, and you may have read the overall introduction of the C standard library in terms 49, then your first reaction must be in the standard library. Set template. Yes, since you can use something written by others, why should I write a new template again?

However, after in-depth research of the SET's help documentation, you will find that the following restrictions below will not meet your program requirements: SET requires elements that are included in it must be completely ordered, ie, for two Elements A and B, must be determined: either a

Specifically, you decided to let your SET template inheritance from List. That is, SET will inherit from List . Because, in your implementation, the SET object is actually a List object. So you declare the Set template:

// SET is erroneously used ListTemplate Class Set: Public List {...};

At this point, everything seems to be correct, but it is actually not small. As explained in Terms 35, if D "is a" B, all facts set to B are also established. However, the List object can contain repetitive elements, so the value of ž3051 is added twice in List , and the LIST will contain two copies of 3051. In contrast, the SET cannot contain repetitive elements, so if 3051 is added to SET , only one copy of this value is included in the SET. So, a SET "is a" List is the sky lie, because as mentioned above, there are some facts found in the List object in the SET object.

Because the relationship between these two classes is not "a", it is a mistake to use public inheritance to indicate their relationship. The correct way is to let the Set object "implement" with list objects ":

{PUBLIC: BOOL MEMBER (Const T & ITEM) Const;

Void INSERT (Const T & ITEM); Void Remove (Const T & ITEM);

Int cardinality () const;

Private: list rep; // represents a set};

The member function of the SET can use the list of List and other parts of the Standard Library, so that the code is not difficult to write:

Template BOOL SET :: Member (const T & item) const {return (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) {list :: itemarator it = Find (rep.begin (), rep.end (), item);

IF (it! = rep.end ()) rep.rase (it);

Template int set :: cardinal () const {return rep.size ();

These functions are simple, so I naturally think of them as inline functions; but before making the final decision, I still review the discussion made by the Terms 33. (In the code above, Find, Begin, End, Push_Back and other functions are part of the standard library basic framework, which can be used to operate such a container template such a List. The overall introduction of the standard library framework See Terms 49 and M35.)

It is worth noting that the interface of the SET class is not complete and minimized (see Terms 18). In terms of integrity, its maximum omission is that it is not possible to loop on the contents of the SET, and this feature is required for many programs (all members in the standard library provide this feature, including SET). Another drawback of SET is that there is no compliance that follows the containers used in the standard library (see Terms 49 and M35), resulting in more difficult to use other parts in the library when using SET.

The interface of the SET has these flaws, but the following cannot be masked: SET has an unconfilled correctness on understanding it and the List. This relationship is not "a" (although the first look is as it is), but "implementation" is implemented by a layering, this relationship is to be proud of the designer.

By the way, when the two classes have been connected by a hierarchy, it is actually established compiled dependencies between two classes. With regard to why do this and how to reduce the trouble of this, see Terms 34.

转载请注明原文地址:https://www.9cbs.com/read-4443.html

New Post(0)