Effective C + +--inheritance and object-oriented design

Source: Internet
Author: User

32. Make sure your public inherits the is-a relationship

Public inheritance means the is-a relationship (the Richter substitution principle), and everything that applies to the base class also applies to derived classes.
The rectangle inherits the square question:

    • Can be implemented with a rectangular operation cannot be implemented with a square
    • In the field of programming. A square is a rectangle that is wrong.
    • In the real world, a square is a rectangle that is correct.
33. Avoid hiding the inherited name
Class Base {Private:intX Public:Virtual void MF1() =0;Virtual voidMF1 (int);Virtual voidMf2 ();voidMF3 ();voidMF3 (Double);}; Class Derived: PublicBase { Public:Virtual void MF1();voidMF3 ();voidMf4 ();};D Erived D;intX;D.MF1 ();//Call DERIVED::MF1D.MF1 (x);//error, as DERIVED::MF1 obscured base::mf1D.mf2 ();//Call BASE::MF2D.MF3 ();//Call DERIVED::MF3D.MF3 (x);//error, as derived::mf3 obscured base::mf3

Inheriting a class function obscures a function of the same name as the base class. Even if the number of parameters is different.
The goal is to prevent inherited classes from inheriting overloaded functions from the base class.

The workaround is as follows:

class Derived:public Base {public:    using Base::mf1;// 让Base class内mf1的全部东西在继承类作用域中都可见    using Base::mf3;    virtualvoid mf1();    void mf3();    void mf4();};

In order for the veil to be uncovered. You can use a using declaration or a transfer function.

34. Differentiate between interface inheritance and implementation inheritance
    1. Interface inheritance
    2. interface and implementation inheritance, overwrite at the same time
    3. interface and implementation inheritance. Do not overwrite
class Shape {public:    virtualvoiddrawconst0;// 接口继承    virtualvoid error(conststring& msg);// 接口和实现继承。同一时候覆写    intobjectconst;// 接口和实现继承,不覆写 };

Agree that the impure virtual function specifies the function declaration and function default behavior at the same time. May cause a crisis.


Class A provides pure virtual functions and implements Fly (). It is expected that inheriting Class B uses fly implementations, and inheriting class C does not use fly implementations. The Class C implementation may not be clear about this convention, causing class C to also use fly.


Workaround:
1. Change fly to pure virtual function. Protected Defaultfly is implemented in Class A.

Class B implements fly in call Defaultfly. Class C implements Fly.
2. Change fly to pure virtual function, Class A provides the definition of Fly, Class B implementation of fly in Call A::fly (), Class C implementation fly

Conclusion:

    • Declaring a pure virtual function expects the inheriting class to inherit only the interface.
    • The purpose of declaring a virtual function is to have an inherited class inherit the interface and default implementation of the function
    • The purpose of declaring a non-virtual function is to let the inheriting class inherit the function's interface and a mandatory implementation
35. Consider alternatives other than the virtual function
    • NVI Non-virtual interface to replace the public Virtual scheme
    • Implement strategy mode by function pointers
    • Classical strategy mode
      Legacy issues:
      Tr1::function use
36. Never again define inherited non-virtual functions

Non-virtual represents invariance override and specificity.

The purpose is to make the interface of inheriting class inheritance function and a mandatory implementation
Use the base class pointer to call the version number of the base class

37. Never redefine the inherited default number of parameters

Different in non-virtual functions intended to be redefined in the inheriting class
Changing the default parameters of an inherited class in a virtual function does not work
The reason for changing the default parameters in a virtual function does not work, and for efficiency reasons, it is slower and more complex to determine the number of parameters at run time than the compiler determines.
Do not provide the default parameters in the virtual function, after the virtual function changes the default parameters, the inheriting class must follow the changes
Use the NVi (Non-virtual Infterace method. The default parameters are provided in the Non-virtual method, and the No-virtaul method calls the virtual method)

class Shape {public:    enum ShapeColor {Red,Green,Blue};    voidconst {        doDraw(color);    }private:    virtualvoiddoDrawconst0;};class Rectangel:public Shape {private:    virtualvoiddoDrawconst;};
38. Through the composite mold out has-a or "according to something to achieve"

Two meanings of compound

    • Has-a (application domain object, such as car, video screen)
    • is-implemented-in-terms-of (Implementing a domain, such as a cache.) Mutual repulsion Device)
39. Judicious and prudent use of private inheritance
    • The assumption is private inheritance, where a base class object is required. An inherited class error is passed in, and the compiler does not voluntarily convert the inheriting class object to a base class object.
    • All members that are inherited by private will become private in the inheriting class
    • Private inheritance means implemented-in-terms-of (based on something)
    • Inheriting classes and base classes have no relationship whatsoever
    • Private inheritance means that the implementation is inherited, and the interface part is omitted. Private inheritance has no meaning at the design level
    • And the trade-offs between the compound
      • Use composite as much as possible
      • Use private inheritance when you need to change a virtual function in the private inheritance base class
      • Another way to override a private inheritance is to change the virtual function by using a compound that defines a nested class that inherits from the public, and the composite class uses that nested object.
class Widget {private:    class WidgetTimer:public Timer {    public:        virtualvoidOnTickconst;    };    

These methods can also be used to prevent inheriting classes from defining the virtual function again.

    • There is also an occasion to use private: EBO empty base optimization white space based optimization (using unary_function,binary_function in STL)

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Effective C + +--inheritance and object-oriented design

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.