Multiple inheritance and virtual base classes, multiple inheritance base classes

Source: Internet
Author: User

Multiple inheritance and virtual base classes, multiple inheritance base classes
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 ().
A problem of multi-inheritance of the virtual base class in c ++

Doesn't virtual inheritance mean that the entire object has only one base1?

Relationship between multi-inheritance and virtual base classes in C ++

During Multi-inheritance, if Class C inherits class B1 and Class B2, both Class B1 and Class B2 inherit Class. In this way, base class A will generate two base class sub-objects in the derived class C.
The virtual base class is used to solve this problem.
If Class A is used as the virtual base class of Class B1 and Class B2, and Class B1 and Class B2 as the virtual base class of class C, base Class A generates only one base class sub-object in the derived class C.
I don't know if you can understand it.

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.