Some summary of virtual inheritance in C + +

Source: Internet
Author: User

1. Why to introduce virtual inheritance

Virtual inheritance is a unique concept in multiple inheritance. The virtual base class appears to resolve multiple inheritance. such as: Class D inherits from class B1, B2, and class B1, B2 inherit from Class A, so the variables and functions in Class A are present in Class D two times. To save memory space, you can define the inheritance of B1, B2 to a as virtual inheritance, and a becomes the virtual base class. The implementation code is as follows:

Class A

Class B1:public virtual A;

Class B2:public virtual A;

Class D:public B1,public B2;

Virtual inheritance is rarely used in general applications, so it is also often overlooked, this is mainly because in C + +, multiple inheritance is not recommended, and is not commonly used, and once left multiple inheritance, virtual inheritance is completely lost the need to exist because it will only reduce efficiency and occupy more space.

2. What is the difference between introducing virtual inheritance and direct inheritance?

Because of the two characteristics of indirection and sharing, it is decided that the objects under the virtual inheritance system will be greatly different in time and space when they are accessed.

2.1 Time: when accessing a member of a virtual base class object (including data members and function members) by inheriting a class object, it must be done through some kind of indirect reference, which increases the reference addressing time (as with the virtual function), in essence, by adjusting the this pointer to point to the virtual base class object. Only this adjustment is run-time completed.

2.2 Space: because of sharing, it is not necessary to save multiple copies of virtual base class sub-objects in object memory, which saves space compared to multiple inheritance. Virtual inheritance differs from ordinary inheritance in that virtual inheritance prevents the occurrence of diamond inheritance when there are two sub-objects of the base class in a derived class. In other words, in order to ensure this, the layout of the base class sub-object is different from the normal inheritance in the case of virtual inheritance. Therefore, it requires a pointer to a base class sub-object.

3. Written test, the knowledge point of C + + virtual inheritance that is often tested in the interview

 virtual                  a class B:p ublic a    }; };      {{class b:public  virtual A      Class b:public a     virtual void foo ();       virtual void foo ();              {                 { };                  };        virtual void foo ();                                virtual void foo ();                }; };

If sizeof (a), sizeof (b) is obtained for each of the four cases. What was the result? Here is the output: (Run in vc6.0) the first: 4,12 the second : The third type: 8,16 Fourth type: 8,8

Think about what this is for?

Because every class that has a virtual function has a 4-byte pointer to its own virtual function table, there should be no problem with the number of bytes of Class A In each case, so how do you calculate the number of bytes in class B? Look at the "first" and "third" case is a virtual inheritance, then there will be a pointer vptr_b_a, this pointer is a virtual class pointer, is also four bytes, but also to include the number of bytes of Class A, so the number of bytes of Class B is calculated. The "second" and "fourth" cases do not include the vptr_b_a, which should be the problem of wood.

4.c++ overloads, overrides, hidden differences and how to perform

Since we are talking about inheritance, I would like to discuss the frequently mentioned overloads, covering and hiding the same range (in the same class) that the 4.1 member function is overloaded (1), (2) The function has the same name, and (3) the parameters are different; (4) The virtual keyword is optional. 4.2 "Overwrite" means that the derived class function overrides the base class function, characterized by: (1) different ranges (in the derived class and the base class, respectively), (2) The function name is the same, (3) The parameter is the same; (4) The base class function must have the virtual keyword. 4.3 "hidden" refers to a function of a derived class that masks a base class function with the same name, characterized by:

(1) If a function of a derived class has the same name as a function of the base class, but the arguments are different, the function of the base class is hidden, regardless of the virtual keyword, (note that it is not confused with overloading). (2) If a function of a derived class has the same name as a function of the base class, but the parameter is the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay).

Summary: Frankly, if the function names and parameters of the derived class and the base class are the same, they are covered, which is understandable, exactly the same, of course, if the function name is the same, the parameters are not the same, it is hidden.

4.4 Three kinds of situation how to perform:

4.4.1 Overloading: See parameters.

4.4.2 Hide: What to invoke with what.

4.4.3 Overwrite: Call derived class.

Ext.: http://blog.csdn.net/skiing_886/article/details/7933402

Some summary of virtual inheritance in C + +

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.