"C + + Common sense" effective C + + terms of Use-memory management/inheritance and object-oriented design/Miscellaneous

Source: Internet
Author: User

Chapter sixth inheritance and object-oriented design

Article 35: The implication of "being one" in public succession

1. Subclass object must be a base class object, the base class object is not necessarily a subclass object

Article 36: Differentiating between interface inheritance and implementation inheritance

1. You want the derived class to inherit only the interface of the member function--Declare the function as a pure virtual function

2. You want the derived class to inherit both the interface and the implementation of the function, but allow the derived class to override the implementation--declare the function as a virtual function

3. Want to inherit interfaces and implementations at the same time and not allow derived classes to overwrite anything-declared as non-virtual functions

Article 37: Never redefine inherited non-virtual functions

1. A function declared as non-virtual by the base class means that the function does not want the quilt class to be modified, but rather directly the quilt class inherits the use

2. Non-virtual functions are statically bound, and when a subclass object is called through a base-class pointer, the function overridden by the subclass object is not called--the non-virtual function of the base class is called

3. If a subclass overrides a non-virtual function of a base class, it is somewhat schizophrenic to call through the subclass object and the pointer, respectively-when the subclass object calls the function, the subclass is called, and if it is called through several classes of pointers, the non-virtual function of the base class is called

Article 38: Never redefine inherited default parameter values

1. Because a non-virtual function that inherits from a redefinition is an error, this bar is equivalent to not "inheriting a virtual function with a default parameter value"

2. Virtual functions are dynamic bindings and the default parameter values are statically bound, and when a subclass object calls a function that has a default value from the form of a base-class pointer, the function of the subclass is called, but the default value is indeed the base class!

Article 39: Avoid "down-conversion" inheritance hierarchies

1. If a pointer to a subclass is converted to a base class to invoke a function that is not inherited from the base class or is an inherited non-virtual function, it can only be achieved by forcing the static transformation of the type----not to use a downward conversion when last resort, making the code unsuitable for maintenance

2. The 1th best solution is to solve the problem by virtual function inheritance

3. If the problem in 1th cannot be solved by virtual function inheritance, try to use the dynamic conversion--dynamic_cast, which can deny the conversion failure, and return a null pointer if the conversion fails

Article 41: Distinguishing between inheritance and templates

1. When the type of an object does not affect the behavior of a function in a class, a template is used to generate such a set of classes

2. When the type of an object affects the behavior of a function in a class, it is necessary to use inheritance to obtain such a set of classes

Article 42: Use private inheritance wisely

1. In contrast to public inheritance, if the inheritance between the two classes is private, the compiler typically does not convert the derived class object to a base class object

2. Members inherited from private base classes become private members of derived classes, even if they are protected or public members in the base class

3. If there is a generic but unsafe class, in order to prevent user misuse of the hidden trouble, you can design the class to give the base class, all its interfaces and member variables declared as protected, and then let other specific classes to privately inherit the class

    

classGenericstack {protected: Genericstack (); ~Genericstack (); voidPushvoid*Object); void*pop (); BOOLEmpty ()Const;Private:    ...                             //Ibid .};                   Genericstack s; //Error! constructor is protectedclassIntStack:PrivateGenericstack { Public:    voidPushint*intPtr)    {genericstack::p ush (INTPTR);} int* POP () {returnstatic_cast<int*>(Genericstack::p op ());} BOOLEmpty ()Const{returngenericstack::empty ();}};classCatstack:PrivateGenericstack { Public:    voidPush (Cat *catptr)    {genericstack::p ush (catptr);} Cat* POP () {returnStatic_cast<cat*>(Genericstack::p op ());} BOOLEmpty ()Const{returngenericstack::empty ();}};i Ntstack is;//correctCatstack CS; //also correct

Article 43: Using Multiple Inheritance wisely

1. If a class inherits from more than one class, and the same member function or member variable exists in its base class, but the subclass does not define the interface or variable, the ambiguity is generated when called by the subclass

classLottery { Public:  Virtual intDraw (); ...};classGraphicalobject { Public:  Virtual intDraw (); ...};classLotterysimulation: PublicLottery, PublicGraphicalobject {...//no claim draw}; Lotterysimulation*pls =NewLotterysimulation;pls->draw ();//wrong!----DifferentiationPls->lottery::d Raw ();//correct-when you explicitly use a class name to limit the modification of a virtual function, the behavior of the function will no longer have a virtual feature. Instead, the called function can only be the one you specify, even if the call is on an object of the derived classPls->graphicalobject::d Raw ();//correct

2. If a CLASSD class inherits from the class ClassA and CLASSB, and the class ClassA and CLASSB both define virtual function Func with the same name, two semantics will occur when the subclass needs to rewrite the 2 Func of Class A and AClass B-- A class allows only one function with the same name, which can be solved by adding the middle layer.

The following is a collection of pure virtual functions, simple virtual functions and inline functions (see clause 33) Integrated application of the method, it is worth keeping in mind:

classLottery { Public:  Virtual intDraw (); ...};classGraphicalobject { Public:  Virtual intDraw (); ...};classAuxlottery: PublicLottery { Public:  Virtual intLotterydraw () =0; Virtual intDraw () {returnLotterydraw ();}};classAuxgraphicalobject: PublicGraphicalobject { Public:  Virtual intGraphicalobjectdraw () =0; Virtual intDraw () {returnGraphicalobjectdraw ();}};classLotterysimulation: PublicAuxlottery, PublicAuxgraphicalobject { Public:  Virtual intLotterydraw (); Virtual intGraphicalobjectdraw (); ...};

Lotterysimulation *pls = new Lotterysimulation;

Lottery *PL = pls;
Graphicalobject *pgo = pls;

Call Lotterysimulation::lotterydraw
Pl->draw ();

Call Lotterysimulation::graphicalobjectdraw
Pgo->draw ();

3. If a subclass inherits from a virtual base class, the initialization parameters of the base class are passed in by the parameters of the child class constructor, and when the inheritance hierarchy is darker it will result in multiple passes of the argument, and if the intermediate inheritance relationship is modified, the class that performs the initialization (the class closest to the virtual base class) may also change, Do not declare member variables in the virtual base class, just provide the interface (Java's approach, its interface--the virtual base class prohibits the inclusion of data)

4. Masonry inheritance of ambiguity: There are classes class A class B Class C class D,b and C inherit from A,d inherit from B and C. If virtual functions mf,b and D are undefined in a and C, ambiguity can be generated when an object through D calls MF:

1. If a is a virtual base class, the C's MF is called because C overrides MF and its precedence is higher than the MF of a.

2. If a is not a virtual base class, ambiguity will be generated-because the function MF is not polymorphic and it will be difficult to choose between A and C when calling MF through D

Chapter Seventh Miscellaneous

Article 45: Figuring out what C + + is doing behind the scenes for you, called functions

1. An empty class, the C + + compiler provides at least one copy constructor, an assignment cloud algorithm, a destructor, a fetch cloud algorithm, a constructor, all of which are common

2. Unless the destructor for the base class of this class is a virtual function, the destructor provided by the compiler is a non-virtual function

3. Default copy constructor and assignment cloud algorithm "member-by-unit" copy (Assignment) for non-static data members of a class

4. For non-static member variables of a class, the default copy constructor and assignment operator call the copy constructor or assignment operator of its member variable class

5. If you do not want to implement operator = or operator&, you can declare it as private

"C + + Common sense" effective C + + terms of Use-memory management/inheritance and object-oriented design/Miscellaneous

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.