Valid tive C ++, 3rd edition, item 39: exercise caution when using private inheritance (private inheritance) (on)

Source: Internet
Author: User

Item 39: exercise caution when using private inheritance (private inheritance)

By Scott Meyers

Translator: fatalerror99 (itepub's nirvana)

Release: http://blog.csdn.net/fatalerror99/

Item 32 discusses how C ++ regards public inheritance (Public inheritance) as an is-a relation. Given a hierarchy (inheritance system), one class student inherits from a class person public. To successfully call a function, students must be implicitly transformed to persons, it demonstrates this to the compiler. It is worthwhile to use private inheritance (private inheritance) instead of public inheritance (Public inheritance) to redo part of this example:

Class person {...};
Class student:PrivatePerson {...}; // inheritance is now Private

Void eat (const person & P); // anyone can eat

Void Study (const student & S); // only students study

Person P; // P is a person
Student s; // s is a student

Eat (p); // fine, P is a person

Eat (s); // error! A student isn' t a person

Obviously, private inheritance (private inheritance) does not mean is-. So what does it mean?

"Hello !" You said, "before we get the meaning of it, Let's first look at its behavior. What is the behavior of private inheritance ?" Well, the first rule that controls private inheritance (private inheritance) can only be seen from the action: Compared with public inheritance (Public inheritance), if the classes (class) the Inter-inheritance relationship (inheritance relationship) is private, and the compiler usually does not set a derived class Object (derived class Object) (such as student) transform to a base class Object (base class Object) (such as person ). This is why calling eat for object (object) s fails. The second rule is that Members (Members) inherited from a private base class (private base class) will become the private members (Private member) of the derived class (derived class ), even if they are protected or public in base class (base class.

This is not the case. This gives us meaning. Private inheritance (private inheritance) means is-implemented-in-terms-of (according ...... ). If you make class (class) d inherit from class (class) B private, you do this because you are interested in using some of the features available in class (class) B, it is not because of the conceptual relationship between types (type) B and the objects (object) of types (type) d. Similarly, private inheritance (private inheritance) is purely an implementation technology. (That's why everything you inherit from a private base class (private base class) is converted into private (private) in your class: all of them are implementation details .) Using the terms proposed in item 34, private inheritance (private inheritance) means that only implementation (Implementation) should be inherited; interface (Interface) should be ignored. If D inherits from B, it means D objects are implemented in terms of B objects (D object is implemented based on B object), no more. Private inheritance (private inheritance) in SoftwareDesign(Software design) period does not make any sense, only in the softwareImplementation(Software implementation.

Private inheritance (private inheritance) means is-implemented-in-terms-of (according ...... The fact is a bit confusing, because item 38 points out that composition (composite) has the same meaning. How do you choose between them in advance? The answer is simple: as long as you can use composition (composite), you can only use private inheritance (private inheritance) when absolutely necessary ). When is it absolutely necessary? When protected members and/or virtual functions are involved, there is also an extreme space-related situation that skews the balance to private inheritance (private inheritance. We will worry about this extreme situation later. After all, it is just an extreme situation.

Suppose we work on an application that contains widgets, and we think we need to better understand how widgets is used. For example, we need to know not only the frequency of widget member functions (member function) calls, but also how call ratios (call rate) changes over time. Programs with clear execution stages can have different behavior focuses at different execution stages. For example, the use and optimization of functions in the parsing phase of a compiler are very different from those in the code generation phase.

We decided to modify the widget class to continuously track how many times each member function (member function) was called. At runtime, We can periodically check this information, which may be associated with the value of each widget and other data that we find useful. To do this, we need to set up some type of timer (timer) so that we can know when the usage statistics are collected.

Try to reuse existing code as much as possible, instead of writing new code. I rummaged through the box in my toolkit and found the following class with satisfaction ):

Class timer {
Public:
Explicit timer (INT tickfrequency );
Virtual void ontick () const; // automatically called for each tick
...
};

This is exactly what we are looking for: a timer object that can set the tick Frequency Based on our needs, and it calls a virtual function (virtual function) every time a tick is made ). We can redefine this virtual function so that it can check the current status of the widget. Perfect!

To redefine a virtual function in timer for widgets, Widgets must inherit from timer. However, public inheritance (Public inheritance) is not suitable in this case. Widget is-a (a) timer is not valid. The customer of a widget should not be able to call ontick on a widget, because it is not a part of the interface (Interface) of a widget. Allowing such a function call will make it easier for the customer to misuse the interface (Interface) of the widget. This is a reference to item 18, "Making the interface Easy to use correctly, the suggestion that is difficult to use incorrectly obviously violates. Public inheritance (Public inheritance) is not the correct option here.

(Click here, next)

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.