Identification and access of C++ derived class members - virtual base class

When some or all of the direct base classes of a certain class are derived from another common base class, the members inherited from the upper common base class in these direct base classes have the same name. In the object of the derived class, these data members with the same name have multiple copies in memory at the same time, and the same function name will have multiple mappings.

You can uniquely identify and access them separately through the scope discriminator, or **set the common base class as a virtual base class. At this time, there is only one copy of the data members with the same name inherited from different paths in memory. There is also only one mapping for a function name. ** This solves the problem of unique identification of members with the same name.

The declaration of the virtual base class is carried out during the definition of the derived class.

1. The general syntax for declaring a virtual base class is:
class 派生类名::virtual 继承方式 基类名

The above statement declares the base class as the virtual base class of the derived class. In the case of multiple inheritance, the scope of the virtual base class keyword is the same as that of the inheritance method keyword, and it only works on the base class immediately following it.

After the virtual base class is declared, the members of the virtual base class maintain the same memory data copy with the derived class during the further derivation process.

[Example] The virtual base class is an example of
a base class B0, which declares the data member v0 and the function member fun0, and class B and class B2 are derived publicly from B0. When deriving, declare B0 as the virtual base class, and then use B1 and B2 as the base Class common public derivation produces a new class D. No new members with the same name will be added in the derived class (if there are members with the same name, the hidden rules will also be followed). At this time, in class D, the members v0 and fun0 in the base class B0 inherited through the two derivation paths of B1 and B2 There is only one copy, and the derivation diagram of the class and the structure of the derived class are as follows:
insert image description here
After using the virtual base class, there is only the only data member v0 in the derived class D. In the module that creates D-type objects, these members can be uniquely identified and accessed by directly using the method of "object name. member name".

The program code is as follows:

#include<iostream>
using namespace std;

class B0
{
    
    
public:
	int v0;
	void fun0()
	{
    
    
		cout << "基类B0的成员" << endl;
	}
};

class B1 :virtual public B0
{
    
    
public:
	int v1;
};

class B2 :virtual public B0
{
    
    
public:
	int v2;
};

class D :public B1, public B2
{
    
    
public:
	int v;
	void fun()
	{
    
    
		cout << "派生类D的成员" << endl;
	}
};

int main()
{
    
    
	D d;
	d.v0 = 2;
	d.fun0();
	return 0;
}

Running results:
insert image description here
Analysis:
The virtual base class declaration only uses the virtual keyword in the derivation process of the class. In the main function, an object d of a derived class D is created, and the members v0 and fun0 of this class can be accessed through the member name.

Comparison between using scope discriminator and virtual base class technology:
when using scope discriminator, there are multiple copies of members with the same name in the derived class, which are uniquely identified by the direct base class name, and different data can be stored and different operation; use virtual base class technique to maintain only one copy of members. In contrast, using scope identifiers can accommodate more data, and using virtual base class technology is more concise and saves memory space.

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/132143680