How to implement virtual inheritance under "Explore" vs-2

Source: Internet
Author: User

In the previous article about the two semantics of the member variables in virtual inheritance and the solution to data redundancy, let's look at how virtual inherited member functions are.

First, will virtual inheritance override the member function? Let's take a look at the following code:

#include <iostream>using namespace Std;class a{public:void Fun () {cout << ' This was fun of A ' <<endl;}}; Class B1:public A{public:void Fun () {cout << ' This was fun of B1 ' << Endl;}; Class B2:public A{public:void Fun () {cout << ' This was fun of B2 ' << Endl;}; Class C:public B1, Public b2{public:void fun () {cout << ' This was fun of C ' << Endl;}}; int main () {C c;c.fun (); C.b1::fun (); C.b2::fun (); C.a::fun ();//compilation does not pass, object does not exist}

Here, which instruction in the main function compiles, is obviously the last one, because the calling object is not clear, C inherits the B1,B2,B1,B2 contains the A,C does not directly contain object A, so the compilation is different because the system does not recognize whether we call a B2 or b1 a, To some extent, it is also the manifestation of ambiguity, in C, we do not need to know a,b1,b2, the fun (), order to know the fun in C (), here we use virtual function rewrite, make fun () rewrite, so we can make C from the various fields of fun () Is it the same as the form of expression? Let's take a look at the result of the virtual function rewrite:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/48/wKiom1bkO1WRn7X7AABAcDgBq6s337.png "title=" Qq20160312235314.png "alt=" Wkiom1bko1wrn7x7aabacdgbq6s337.png "/>

The answer is no, the virtual function override simply overwrites the member function and does not cause the member function in the derived class to be called "destroyed" in the base class. Derived classes can still access member functions in the base class through the domain. What if we use virtual inheritance?

The answer is still not possible:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/48/wKiom1bkPeShvt55AABBTImrJsA143.png "title=" Qq20160313000413.png "alt=" Wkiom1bkpeshvt55aabbtimrjsa143.png "/>

Open the Watch window, we found that C contains 3 virtual table pointers, each virtual table pointer is the address of c::fun (), but still can be accessed through the domain to a different fun (), if we add a member variable in a, through memory observation, (this is the same as we mentioned in the previous article, In order to solve the data redundancy, virtual inheritance makes the superclass of the diamond inheritance only one copy of the member variables stored in the derived class, the two base classes access the same data with the pointer offset, under VS2013, and does not store the true base class member variable, which is an optimization ), in the previous VS version, Potentially derived classes can also hold data for member variables in the base class. The member function, not the content stored in the object, is accessed through the domain, or can be modified.

This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1750438

How to implement virtual inheritance under "Explore" vs-2

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.