Multiple inheritance and virtual base classes

Source: Internet
Author: User
Multiple inheritance of implicit classes describes classes with multiple direct base classes. Multi-inheritance brings two main problems:
① Inherit the same name method from two different base classes;
② Inherit multiple instances of the same class from two or more related base classes. For example:
Class
{
PRIVATE:
String name;
Int ID;
Public:
Virtual void F ();
...
};
Class B: public
{
PRIVATE:
Char ch;
Public:
Void F ();
...
};
Class C: public
{
PRIVATE:
Double dB;
Public:
Void F ();
...
};
Class D: Public B, public C
{
Public:
Void F ();
...
};
The following section describes the ambiguity:
D;
A * A = & D;
Generally, this assignment sets the base class pointer to the address of the base class object in the derived object. But D contains two A objects and two addresses are available for selection. Therefore, type conversion should be used to specify the objects:
A * a1 = (B *) & D;
A * a2 = (C *) & D;
This will complicate the use of base class pointers to reference different objects.
When C ++ introduces multi-inheritance, it introduces a new technology-virtual base class, which is the possibility of Multi-inheritance. The virtual base class allows objects derived from multiple classes (with the same base class) to inherit only one base class object. For example, by using the keyword virtual in the class declaration, A can be used as the virtual base class of B and C (the order of virtual and public does not matter ):
Class B: virtual public {...};
Class C: virtual public {...};
When using a virtual base class, you need to adopt a new method for the class constructor. For non-virtual base classes, the only constructor that can appear in the initialization list is the real-time base class constructor. However, these constructors may need to pass information to their base classes. For example,
Class base {...};
Class derived1: public base {...};
Class derived2: Public derived1 {...};
The constructor of the derived2 class can only call the constructor of the derived1 class, while the constructor of the derived1 class can only call the constructor of the base class.
If a is a virtual base class, automatic transmission of such information will not work. For example, for the following multi-inheritance constructor:
D (const A & A, char C, double B)
: B (a, c), C (a, B ){}
The problem is that when information is automatically transmitted, A is passed to object A through two different channels (B and C. To avoid such conflicts, when the base class is virtual, C ++ prohibits information from being transmitted to the base class through the intermediate class. Therefore, the above constructor will initialize the members CH and DB, but the information in a will not be passed to object. However, the compiler must construct the base class object component before constructing the derived object; in the above case, the compiler will use the default constructor of. If you do not want the default constructor to construct the base class object, you need to explicitly call the required base class constructor. Therefore, the constructor should be like this:
D (const A & A, char C, double B)
: A (a), B (a, c), C (a, B ){}
Note: This method is valid. This is required for the virtual base class, but it is invalid for non-virtual base classes.
Which method is used?
For single inheritance, it is acceptable to let the derived method call the method of the base class. For example, for method F () in B ():
Void B: F () {A: F ();}
However, this method is invalid for the D sample. Method:
Void D: F () {B: F ();} is invalid because it ignores the C component. You can remedy the problem by simultaneously calling F () of Version C:
Void D: F () {B: F (); C: F ();} but it is likely to do the same job. So how can this problem be solved? The modular approach, rather than the incremental approach, provides a method that only explicitly represents component A and a method that only displays component B or component C, and then combines components in method D.
Hybrid use of virtual base classes and non-virtual base classes: When a class inherits a specific base class through multiple virtual and non-virtual channels, this class will contain a base class sub-object that represents all virtual paths and a sub-table that represents multiple base class sub-objects of each non-virtual path.
If a class inherits more than two members with the same name from different classes, when this member name is used, if the class name is not qualified, it will lead to ambiguity. However, if you use a virtual base class, this does not necessarily lead to ambiguity. In this case, if a name takes precedence over all other names, it will not lead to ambiguity even if it is not used.
How does one member name take precedence over another? The names in the derived class take precedence over the same names in the direct or indirect ancestor classes. For example, in the following definition:
Class B
{
Public:
Short Q ();
...
};
Class C: virtual public B
{
Public:
Long Q ();
Int OMB ();
...
};
Class D: Public C
{...};
Class E: virtual public B
{
PRIVATE:
Int OMB ();
...
};
Class F: Public D, Public E
{
...
}
The Q () Definition in Class C takes precedence over the Q () Definition in Class B, because Class C is derived from Class B. Therefore, the method in F can use Q () to represent C: Q (). Any OMB () definition does not take precedence over other OMB () Definitions, because C and E are not the peer base classes. Therefore, using non-limited OMB () in F will lead to ambiguity.
Virtual binarization rules have nothing to do with access rules. That is to say, even if E: OMB () is private, it cannot be accessed directly in Class F, but OMB () is used () it will still lead to ambiguity. Similarly, even if C: Q () is private, it will take precedence over B: Q (). In this case, you can call B: Q () in Class F. However, if Q () is not limited, a C :: q ().

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.