Another discussion on C + + inheritance

Source: Internet
Author: User

One or three ways of inheriting

Public members of private base classes and protected members become private members of derived classes

member access rights for public base class are not changed

Protected the public and protected members of the base class will be protected members of the derived class

Comparison of two or three ways of inheritance:

Third, in the absence of virtual functions, the class pointer call function is to note:

    • If you point to a derived class object with a base-class pointer, only the function defined by the base class can be called through the pointer
    • If you point to a base class object with a pointer to a derived class, you must first make an explicit cast, but it is dangerous
    • If both the base class and the derived class define member functions of the same name, the function called by the object pointer is determined by the original type of the pointer, rather than by the type of the object pointed to by the pointer.

Four, virtual function table:

Defined:

To achieve the purpose of dynamic binding (late binding), the C + + compiler, through a table, "indirectly" invokes the function that is actually bound (note the word "indirect") in the execution period. Such tables are called virtual function tables (often referred to as vtable). Each "class containing virtual functions", The compiler makes a virtual function table for it, and each element in the table points to the address of a virtual function. In addition, the compiler will of course add a member variable to this class, a pointer to the virtual function table (often referred to as vptr), and each derived object will have this one vptr.

Introduced:

When we call virtual function through this object, we actually find the virtual function through Vptr, then find out the real address, virtual function table in this indirect way, the content of the virtual function table is based on the order of the virtual function declaration in the class, the meaning fills in the function table (and all other members that can inherit). When we overwrite a virtual function in a derived class, the virtual function table is affected: the function address that the element in the table refers to is no longer the function address of the base class, but the address of the derived class

Single inheritance

Declaration format:

Class Derived classes name: The base class name {...} if not specifically indicated by default to private)

Characteristics:

Inherits all properties and behaviors of the base class, including private members, but does not allow derived classes to access the base class private members directly

constructor function:

Format: derived class constructor name (formal parameter list): base class constructor name (formal parameter list) {...}

Points:

When you create a derived class object, the program first creates the base class object, that is, the base class object should be created before entering the derived class constructor (that is, calling the base class constructor first, and then invoking the derived class constructor) the derived class constructor should pass the base class information to the base class constructor through the member initialization table Derived class constructors should initialize new data members of derived classes

Destructors:

Attribute: When a derived class object expires, the program calls the derived class destructor first and then calls the base class's

Virtual destructor:

Function: Similar to virtual function, when releasing a derived class object with a base class pointer, in order to invoke the correct destructor.

Note: When a class has a virtual function, it is often used as a base class, and its derived classes are often allocated using new, it is best to also use a virtual destructor, because this ensures that the correct destructor is invoked when the instance object is disposed

Attention:

    • Constructor call order is derived class, base class
    • The call order of destructors is derived class--base class
Multiple inheritance

Declaration format:

Class Name: base class name, inheritance mode base class name ... {....};

constructor function:

    • Form:

Derived class Name:: derived class name (formal parameter list): base class name (formal parameter list), base class name (formal parameter list) ... {...}

    • Characteristics:

The order of execution of base class constructors that handle the same layer depends on the order in which the derived class is defined for each base class, regardless of the order in which the base class is ordered when the constructor for the derived class is defined

    • Two definitions:

Reason:

    • Because of the multi-layered cross-derived class relationship, a derived class object contains multiple sub-bodies of the base class member
    • A member name is the same in more than one base class

Method:

    • The virtual base class is used to solve the multi-layered cross-derived class relationship (principle: defining a derived class with a virtual base class definition, when a derived class is created, a member of a virtual base class in the class hierarchy retains only one, that is, a copy of the virtual base class member is shared by all derived classes)
    • Use the access modification method of a member defined in a base class to resolve a member name that is the same in multiple base classes

Virtual base class:

Declaration format: Class derived class Name: Virtual inheritance Mode base class name,... { ... };

Attention:

    • Virtual is independent of the order of inheritance and uses the base class as the virtual base class, and the computer does some extra computational work to do this with caution
    • A class can be used as a virtual base class in a class family, or as a non-virtual base class.
    • In the object of a derived class, a virtual base class with the same name produces only one virtual base class sub-object, and a non-virtual base class produces its own object.
    • The virtual base class sub-object is initialized by the constructor of the most derived class by calling the virtual base class's constructor.
    • The most derived class refers to the class that is specified when an object is created in the inheritance structure.
    • In the member initialization list of a constructor for a derived class, a call to the virtual base class constructor must be listed, and if not listed, the default constructor for the virtual base class is used.
    • A constructor in a derived class that derives directly or indirectly from a virtual base class in the member initialization list, lists calls to virtual base class constructors. However, only the constructor of the most derived class that is used to establish the object calls the constructor of the virtual base class, and the call to the virtual base class constructor listed in all base classes of the derived class is ignored in execution. This guarantees that the virtual base class sub-object is initialized only once.
    • When a call to a virtual base class and a non-virtual base class constructor occurs in a member initialization list, the constructor of the base class is executed before the constructor of the non-virtual base class

There is a difference between a virtual base class and a non-virtual base class:

    • There are virtual base classes:

The order of the constructors is related to the order of inheritance, not to the call of the base class constructor

Another discussion on C + + inheritance

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.