Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand inheritance and virtual inheritance in C++

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to understand inheritance and virtual inheritance in C++? for this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Common public inheritance class test1 {public: test1 (int I): num1 (I) {} private: int num1;}; class test2: public test1 {public: test2 (int iMint j): test1 (I), num2 (j) {} private: int num2;}; void main () {test2 T2 (1Magin2);}

(test2 memory structure) check the memory and find that the parent class is above the subclass.

Multiple inheritance

Add the test3 class to the original code

The test3 class inherits test2 and test1

Class test1 {public: test1 (int I): num1 (I) {} private: int num1;}; class test2 {public: test2 (int I): num2 (I) {} private: int num2;}; class test3: public test2, public test1 {public: test3 (int I, int jint k): test1 (I), test2 (j), num3 (k) {} private: int num3;}; void main () {test3 T3 (1, 2, 3) }

(test3 memory address) is still the parent class on the subclass

But now there are two parent classes. Why is test2 on test1?

It has something to do with our inheritance order. We inherit test2 and then we inherit test1. Changing the inheritance order of memory will also change.

Virtual inheritance class test1 {public: test1 (int I): num1 (I) {} private: int num1;}; class test2: virtual public test1 {public: test2 (int ireint j): test1 (I), num2 (j) {} private: int num2;}; void main () {test2 T2 (1,2);}

(T2 memory) We find that after virtual inheritance, the parent class member data has inexplicably more four bytes at the first address under the subclass member data.

This four-byte is the address of our virtual base class table.

Follow the virtual inheritance table where the difference between this class and the parent object can be found through the difference.

Let's take a look at this memory 0x0082fbd8 is the first address of T2 and 0x0082fbe0 is the location of the parent class.

0x0082fbd8-0x0082fbe0 = = 8

Is the difference between this class and the parent object

Virtual inheritance (diamond inheritance) class test1 {public: test1 (int I): num1 (I) {} private: int num1;}; class test2: virtual public test1 {public: test2 (int iMint j): test1 (I), num2 (j) {} private: int num2;}; class test3: virtual public test1 {public: test3 (int I int j): test1 (I), num3 (j) {} private: int num3;} Class test4: public test2, public test3 {public: test4 (int I, int j, int k): test1 (I), test2 (iMaginj), test3 (iMaginj), num4 (k) {} private: int num4;}; void main () {test4 T4 (1,2recover3);}

In the memory of test4, we can see that both T2 and T3 have their own virtual base class table address to record the offset between themselves and the parent class.

The contents of the two virtual base class tables

Now let's calculate whether the difference to the grandfather class is correct.

0x00FAFD50-0x00fafd3c = = 14

0x00FAFD50-0x00fafd44 = = C

So much for the answers to the questions about how to understand inheritance and virtual inheritance in C++. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report