"C + +" virtual base class

Source: Internet
Author: User

Where to use the virtual base class:

Why avoid ambiguity caused by multiple public base classes in multi-layer inheritance

Virtual base class Usage

When a derived class inherits a base class, the virtual base class is inherited by the addition of an Vsan keyword.

In the program run, we find that the class bass constructor is only called once, so obj.a will not produce ambiguity.

Question 1:

Here we have to pay special attention to the results of the OBJ.A 123, why it is not 122 is not 124, it is 123?

The order in which the constructor is called:

  

As can be seen from the results, we find that grand first calls the constructor of the virtual base class Derive_v, and for Derive_v, it also starts to call the virtual base class Base_v function First, that is, the output "CONS base_v", Then call the base class base constructor output "CONS base", and then output "CONS derive_v", then Grand began to call its base class derive, should be able to obtain the output "CONS base_v", "CONS base" but class Base_ V is the virtual base class, which is the first invocation of the base class.

In the following example we are further explaining the order of the constructor calls

Although the derived class B and the derived class C have a common virtual base class, the two classes are independent of each other, and because B is a priority to create C the call order is a,b,a,c;

Then we proceed to the following output, and we find that class J does not appear in the output. As can be seen from the lowest derived class D, there are two base classes, two sub-objects in D, but note that "j*ptr" only declares a pointer, not an object, that is, D has only one sub-object "M", so the constructor in J is not called and therefore there is no "j" in the output; back to the result, We found that base class A is the first to be called, and that it is assigned a value of. We continue to look up from the bottom derived class D and find that the declaration of base class B precedes the base class C , that is, the base class B To be called before the base class C , we already know that base class B has a virtual base class A, which is the first call of the virtual base class A . then we look at the second base class C of the derived class D , and for this base class we have the result that it does not call the virtual base class Athat is common to B, but only calls its own imaginary function. , once again, the virtual base class only invokes the conclusion once.

"C + +" virtual base class

Related Article

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.