Effective C + +: Clause 39: Use private inheritance wisely and prudently

Source: Internet
Author: User

A

(1) Private succession means "to achieve from something" . Only implementations are partially inherited. The interface part should be omitted;

(2) It is only meaningful at the "implementation" level of the software, which is meaningless at the software "design" level.

(3) The base class members that are inherited by private will become private in the derived class, even though they are originally protected or public properties in the base class;

(4) Assume that the class is a private inheritance relationship. The compiler does not actively convert a derived class object to a base class object on its own.

(5) Class D inherits the Class B in private form, meaning. The D object is implemented according to the B object, and there is no other implication.


Two

private inheritance and recombination are "based on something to achieve", use composition as much as possible at design time and use private inheritance sparingly .

The reasons are:

(1) Private inheritance can once again define virtual functions in the base class (even if they cannot be called), and in many cases it should be blocked. and compound very easy to control the access rights of members.

(2) Composite can reduce compilation dependency.

but. Private inheritance may be required in the case of so-called "Blank base class Optimization" (EBO): For a member of an empty class object that does not include any member variables and member functions, it is assumed that a composite invocation would consume some memory (C + + provisions Any independent (non-affiliated) object must have a non-0 size , note that it does not apply to the base class component in the derived class, and does not consume memory in the case of inheritance. Such a situation is very rare, in most cases it should follow the "prefer to use composite, do not use private inheritance" principle.


Class Empty {};class holdsanint {private:int x; Empty e;};
sizeof (Holdsanint) > sizeof (int). In most compilers, the size of sizeof (Empty) is 1.Because of the "zero-size independent object". Usually the C + + official ordered to silently insert a char into the empty object.

However, the alignment requirements (alignment) may cause the compiler to add some padding (padding) to a class like holdsanint, so it is possible that the Holdsanint object is not just more than one char size, but actually magnified to one more int.

Independent (non-affiliated) This constraint does not apply to the base class component within the derived class object, since they are not independent. Suppose you inherit empty, not an object of that type:
Class Holdsanint:private empty{private:     int x;};
almost able to determine sizeof (holdsanint) = = sizeof (int).

So in this form, you can save memory.


(iii)If there are timers timer classes:
Class Timer {public:    explicit timer (int tickfrenquency);    virtual void OnTick () const;    ...};
< Span style= "FONT-FAMILY:SIMSUN; font-size:14.44444465637207px; line-height:21.111112594604492px "> If a new class widget wants to use the functionality of Ontick (). Can adopt the method of inheritance: Because the public inheritance is is-a relationship, but there is no such relationship between them! And should not let customers see

:

(1) Adopt private inheritance.


(2) methods of inheriting and compounding with public:

Class Timer {public:    explicit timer (int tickfrenquency);    virtual void OnTick () const;    ...}; Class Widget{private:    class widgettimer:public timer{public    :        virtual void OnTick () const;        ...    };    Widgettimer timer;    ...};
< Span style= "FONT-FAMILY:SIMSUN; font-size:14.44444465637207px; line-height:21.111112594604492px "> It used public inheritance and compositing at the same time, and introduced a new class Widgettimer, so that < Span style= "FONT-FAMILY:SIMSUN; font-size:14.44444465637207px; line-height:21.111112594604492px "> (1) prevents the widget's derived class from defining the virtual function in the timer again, widget derived class will not be able to take widgettimer, so it cannot inherit it or define its virtual function again ; (2) Reduce compilation dependencies to the minimum.

See clause 31.


< Span style= "FONT-FAMILY:SIMSUN; font-size:14.44444465637207px; line-height:21.111112594604492px ">
< Span style= "FONT-FAMILY:SIMSUN; font-size:14.44444465637207px; line-height:21.111112594604492px ">  please remember:
(1) Private inheritance means is-implementd-in-terms-of (according to something). It is usually lower than the compound (composition) level.

This is reasonable when derived class needs to access the members of the protected base class, or it needs to define the inherited virtual functions again.

(2) different from compound (compoistion). Private inheritance can result in the optimization of empty base.

This can be important for library developers who are placed in the "Minimize object size."





Effective C + +: Clause 39: Use private inheritance wisely and prudently

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.