Confusing knowledge points of C ++ 4 --- virtual functions and virtual inheritance

Source: Internet
Author: User

In object-oriented C ++, virtual functions and virtual inheritance are completely different concepts. 1. As long as the class contains virtual functions in the virtual function C ++ program, the compiler will generate a corresponding virtual function jump table (vtbl) for this class ), this virtual function jump table is a linear table consisting of multiple virtual function body entry addresses. The first half of the jump table from the virtual function of the derived class is obtained from the vtbl of the parent class, but the content in the table is not necessarily the same. The second half corresponds to the new virtual function. Copy code class Employee {protected: char * Name; int Age; public: void changeAge (int newAge); virtual void retire (); // virtual function Employee (char * n, int );~ Employee () ;}; class Manager: public Employee {int Level; public: void changeLevel (int l); void retire (); // Case 1: The subclass overwrites the virtual function Manager (char * n, int a, int l );~ Manager () ;}; copy the code. 1. If the subclass overwrites the virtual function of the parent class, the virtual function tables of the Child class and the parent class are: case 2: If the subclass does not overwrite the virtual function of the parent class, the virtual function table of the subclass and the parent class is: copy the code class Manager: public Employee {int Level; public: void changeLevel (int l); // void retire (); // case 2. The subclass does not overwrite the virtual function Manager (char * n, int a, int l );~ Manager () ;}; copy the code. 3. Copy the virtual function of the subclass. The virtual function table of the subclass and parent class is: copy the code class Manager: public Employee {int Level; public: virtual void changeLevel (int l); // case 3. The subclass has its own virtual function void retire (); Manager (char * n, int a, int l );~ Manager () ;}; copy code 2. Virtual inheritance virtual Inheritance occurs to solve problems in multiple inheritance. To understand the implementation mechanism of virtual inheritance, first look at General inheritance: copy the Code class A {char k [3]; public: virtual void aa () {};}; class B: public A {char j [3]; public: virtual void bb () {};}; class C: public B {char I [3]; public: virtual void cc () {};}; int main () {cout <sizeof (A) <endl; cout <sizeof (B) <endl; cout <sizeof (C) <endl; return 0;} The answer obtained by copying the code is as follows: 8 12 16: 1. for Class A, there is A virtual function, A virtual function table vtbl is required to record the function entry address. Each address has a virtual pointer and the pointer size is 4. The class also has a member variable char k [3]. you can obtain The size of sizeof (A) is 8. 2. for Class B, there is also A virtual function. A virtual function table vtbl is required to record the function entry address, each address has A virtual pointer. the pointer size is 4. There is also A member variable j [3] in the class. However, class B inherits from Class A and is generally inherited, it is not a virtual inheritance, so we can get the sizeof (B) Size: 4 (Virtual pointer pointing to the virtual function) + 4 (your own data member) + 4 (data member of parent class A) = 12; 3. for Class C, there is also A virtual function. A virtual function table vtbl is required to record the function entry address, each address has a virtual pointer. the pointer size is 4. There is also a member variable I [3] in the class. Similarly, Class C inherits from Class B and is generally inherited, it is not a virtual inheritance, so we can get the sizeof (C) Size of 4 (Virtual pointer pointing to the virtual function) + 4 (own data member) + 8 (parent class data member) = 16; let's take A look at the virtual inheritance: copy the Code class A {char k [3]; public: virtual void aa () {};}; cl Ass B: public virtual A {char j [3]; public: virtual void bb () {};}; class C: public virtual B {char I [3]; public: virtual void cc () {};}; int main () {cout <sizeof (A) <endl; cout <sizeof (B) <endl; cout <sizeof (C) <endl; return 0;} copy the code to get the answer: 8 20 32 analysis: 1. for Class A, there is A virtual function, A virtual function table vtbl is required to record the function entry address. Each address has a virtual pointer, And the pointer size is 4. The class also has a member variable k [3]. according to the data alignment principle, we can obtain that the size of sizeof (A) is 8. 2. for Class B, there is also A virtual function, A virtual function table vtbl is required to record the function entry address. Each address has a virtual pointer and pointer. Is 4, and there is A member variable j [3] in the class, but class B inherits from Class A and is virtual inheritance, the implementation of virtual inheritance is to use a list of virtual base class pointers. For example, vptr_ B _A points to the virtual base class and also contains all the content of the parent class, so we can get sizeof (B) the size is: 4 (Virtual pointer pointing to your own virtual function) + 4 (data member) + 4 (pointer pointing to the virtual base class vptr_ B _A) + 8 (content size of parent class A) = 20; 3. for Class C, there is also A virtual function that requires A virtual function table vtbl to record the function entry address, each address has a virtual pointer. the pointer size is 4. There is also a member variable I [3] in the class. Similarly, Class C inherits from Class B and is a virtual inheritance, the implementation of virtual inheritance is to use a list of virtual base class pointers. For example, vptr_C_ B points to the virtual base class and also contains all the content of the parent class, so we can get sizeof (C) the value is 4 (the virtual pointer pointing to the self-built function) + 4 (the data member of the self-built function) + 4 (the pointer pointing to the virtual base class (vptr_C_ B) + 20 (content size of parent class B) = 32; copy the code Class A {char k [3]; public: virtual void aa () {};}; class B: public virtual A {char j [3]; public: virtual void bb () {};}; class C: public virtual A {char I [3]; public: virtual void cc () {};}; class D: public B, public C {char n [3]; public: virtual void dd () {};}; int main () {cout <sizeof (A) <endl; cout <sizeof (B) <endl; cout <sizeof (C) <endl; cout <sizeof (D) <endl; return 0 ;} the result of copying the code is as follows: 8 20 20 36: 1. sizeof (A, B, C), the result obtained from the previous analysis is 8 20 20; 2. sizeof (D) = 4 (Virtual pointer pointing to your own virtual function) + 4 (your own data member) + 12 (content of parent class B, including data members, virtual pointers to virtual functions, and virtual base class pointer list vptr_ B _A) + 12 (parent class C content, including data members, virtual pointers pointing to virtual functions, and virtual base class pointer list vptr_C_A) + 4 (data member of Class A) = 36. It can be seen that Class D contains only one copy of Class A, and virtual inheritance solves the problem of ambiguity in multiple inheritance.

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.