Effective C ++ 2e Item44

zhaozj2021-02-11  273

Terms 44: Say you want to say; understand what you said

In this chapter, in the introduction of "Inheritance and Object-Oriented Design", I emphasized that understanding the meaning of different object-oriented components in C is important. This is very different from only the rules of the C language. For example, C rules say that if class D is inherited from class B public, there is a standard conversion from the pointer to B of D. The public member function of B will be inherited to D's public member function, and so on. These rules are correct, but in the process of converting design ideas into C , they do not have any effect. Instead, you need to know that public inheritance means "it is a", if D is inherited from B, each object of type D is "a" type B object. Therefore, if you want to indicate "Yes" in the design, you will naturally think of using public inheritance.

"Say what you want to say" is just half of success. The other side of the matter is "Understanding what you said", this is equally important. For example, the membership function declaration is a restriction on the child class, if you don't realize this, you will be unrecognized behavior ---- Unless you are totally intended to do. Declare a non-virtual member function, you actually say that this function has expressed a particularity of the non-variability; if you don't understand this, it will bring a disaster to the program.

Public inheritance and "Is a" equivalent, as well as the equivalence of non-virtual member functions and "non-variability on specialty", is an example of how C components correspond to design ideas. The following list summarizes several of these correspondences.

· The common base category means common characteristics. If class D1 and class D2 declare class B declared as base classes, D1 and D2 will inherit common data members and / or common member functions from b. See Terms 43. · Public inheritance means "it is a". If class D is inherited in class B, each object of type D is also an object of a type B, but it is not true. See Terms 35. · Private inheritance means "implementation with ...". If class D privately inherits to class B, the object of type D is intended to be implemented with the object of type B; there is no conceptual relationship between the objects of type B and Type D. See Terms 42. · The layering means "there is a" or "use ... to implement". If class A contains a data member of type B, the object of type A either has a component of a type B, or the object of type B is used in the implementation. See Terms 40.

The following correspondence only applies to public inheritance:

· The pure virtual function means only the interface of the function. If class C declares a pure virtual function MF, the subclass of C must inherit the interface of the MF, and the specific subclasses of C must provide their own implementation. See Terms 36. · Simple virtual functions means that the interface of the inheritance function plus a default implementation. If class C declares a simple (non-pure) virtual function MF, the subclass of C must inherit the interface of the MF; if desired, you can also inherit a default implementation. See Terms 36. · The non-virtual function means the interface of the inheritance function plus a forced implementation. If class C declares a subclass of non-virtual functions MF, C, must simultaneously inherit the interface and implementation of the MF. In fact, MF defines the "specificity of the speciality" in C. See Terms 36.

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

New Post(0)