Objective C ++ part6.inheritance and object-oriented design

Source: Internet
Author: User
32. Make sure public inheritance models "is-a". The so-called optimal design depends on what the system wants to do, including the present and future. The problem to be solved: there are some questions about the locations where both assert passes. Summary: "Public inheritance" means is-. Everything that applies to base classes must also apply to derived classes, because every derived class object is also a base class object. 33. Avoid hiding inherited names. Conclusion: first, the name in Derived classes hides the name in base class. No one ever wishes to do this under public inheritance. Second, you can use the using declarative or forward function (forwarding functions) to make the masked name goodbye to the day ). Annotation: the transfer function jumps to the member function of the parent class in the member function of the subclass. It only adds a layer of packaging. 34. differentiate between inheritance of interface and inheritance of implementation.1) declares a pure virtual function to allow Derived classes to inherit only function interfaces. 2) the purpose of declaring a simple (non-pure) impure virtual function is to let Derived classes inherit the interface and default Implementation of the function. 3) the purpose of declaring a non-virtual function is to make Derived classes inherit the function interface and a mandatory implementation. Conclusion: first, interface inheritance and implementation inheritance are different. Under Public inheritance, derived classes always inherits the base class interface. Second, the pure virtual function only specifies the interface inheritance. Third, simple (non-pure) impure virtual functions specify interface inheritance and default implementation inheritance. Fourth, non-virtual functions define interface inheritance and mandatory implementation inheritance. 35. Consider alternatives to virtual functions. The customer indirectly calls the private virtual function through the Public Non-virtual member function, which is called the non-Virtual Interface (nvi) method. It is a unique manifestation of the so-called template method design pattern (not associated with C ++ templates. 1) using the non-Virtual Interface (nvi) method is a special form of the template method design pattern. It uses public non-virtual member functions to wrap virtual functions with lower accessibility (private or protected. 2) Replace virtual functions with "function pointer member variables", which is a form of decomposition in the strategy design mode. 3) Replace the virtual function with the tr1: function member variable, so that any callable entity can be used with a signature that is compatible with requirements. This is also a form of strategy design patterns. 4) Replace the virtual functions in the inheritance system with the virtual functions in the other inheritance system. This is a traditional implementation method of the strategy design model. Conclusion: first, alternative solutions for virtual functions include nvi methods and multiple forms of strategy design patterns. The nvi method itself is a special form of template method design mode. Second, moving a function from a member function to a class external function has the disadvantage that non-member functions cannot access non-public members of the class. Third, tr1: the action of a function object is like a normal function pointer. Such an object can accept all callable entities that are "compatible with the given target signature ). Note: This clause describes how to implement virtual functions in a centralized manner. It consists of nvi and strategy. 36. Never redefine an inherited non-virtual function. Conclusion: Do not redefine the inherited non-virtual function. Annotation:
The main reason is that if you do this, first, you destroy the original intention that the non-virtual function should be fully inherited. Second, this will lead to confusion. So this is an unreasonable situation. Do not declare it as virtual, or do not redefine it. Otherwise, the confusion is that when an object executes a member function, it depends on their declared type, rather than its own type. 37. Never redefine a functions's inherited default parameter value. The so-called static type of the object is the type it uses when declared in the program. The so-called dynamic type of an object refers to the type of the object currently referred ". Summary: Do not redefine an inherited default parameter value, because the default parameter values are static bindings, while the virtual function-the only thing you should override-is dynamic binding. Note: most problems are caused by dynamic binding and static binding. This can be analogous to virtual functions and non-virtual functions. 38. Model "has-a" or "is-implemented-in-terms-of" Through composition. Conclusion: first, the meaning of composition is different from that of public inheritance. Second, in application domain, combination means has-a (with one ). In implementation domain, composite means is-implemented-in-terms-of (implemented based on something ). 39. use private inheritance judiciously. both composite and private inheritance mean is-implemented-in-terms-of, but composite is easier to understand. Therefore, you should choose composite whenever you can. Conclusion: first, private inheritance means is-implemented-in-term-of (implemented based on something ). It is generally lower than the composition level. However, this design is reasonable when the derived class needs to access members of the protected base class or to redefine the inherited virtual function. Second, unlike composition, private inheritance can optimize empty base. This may be important for library developers who are committed to minimizing object sizes. Annotation: focuses on private inheritance. 40. Use multiple inheritance judiciously. Conclusion: first, multi-inheritance is more complex than single inheritance. It may lead to new ambiguities and the need for virtual inheritance. Second, virtual inheritance will increase costs such as size, speed, initialization (and assignment) complexity. If the virtual base class does not contain any data, it will be of the most practical value. Third, multi-inheritance does have a legitimate purpose. One of the plots involves the combination of "public inheriting an Interface Class" and "Private inheriting a class that is assisted in implementation.
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: 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.