当前位置:网站首页>Inherit, inherit, inherit
Inherit, inherit, inherit
2022-07-27 00:30:00 【Sy++】
Catalog
The call order of construction and destruction in inheritance
Handling method of members with the same name in inheritance
Static member feature in inheritance
The principle of virtual inheritance to solve ambiguity
In virtual inheritance, variables are accessed through pointers
Inherit
The role of inheritance : Code reuse and functions of extension classes
Private rights : Accessible within class , Out of class instantiation 、 Subclasses are not accessible
Protection rights : Accessible within class , Subclasses can access , Out of class instantiation is not accessible
Public authority : Accessible within class , Subclasses can access , Out of class instantiation is accessible

Look at the object model
Start —VS—Developer Command Prompt for VS 2019—cd To view .cpp File directory — Input :cl/d1 reportSingleClassLayout Class name Full file name
The call order of construction and destruction in inheritance
There are the following codes :
class A
{
public:
A()
{
cout << "A Constructor for " << endl;
}
~A()
{
cout << "A Destructor of " << endl;
}
public:
Son s;
};
class B
{
public:
B()
{
cout << "B Constructor for " << endl;
}
~B()
{
cout << "B Destructor of " << endl;
}
};
class C
{
public:
C()
{
cout << "C Constructor for " << endl;
}
~C()
{
cout << "C Destructor of " << endl;
}
};
class D:public A
{
public:
D()
{
cout << "D Constructor for " << endl;
}
~D()
{
cout << "D Destructor of " << endl;
}
public:
B b;
C c;
};
void test02()
{
D d;
}
The order is : Constructor for the parent class — Constructor of member object — Its own constructor —( End of the function )— Its own destructor — Destructor of member object — The destructor of the parent class
Construction starts with ancestor classes , Re parent class , And then the object , Then itself , Deconstruction is the reverse , Similar to stack , First, push the construction of the parent class and the construction of the member object onto the stack , Then pop up .
Handling method of members with the same name in inheritance
1. When the subclass and parent have Member with the same name when , Members of subclasses with the same name will hide ( Not cover ) A member of the parent class with the same name
class Father
{
public:
Father()
{
a = 10;
}
public:
int a;
};
class son :public Father
{
public:
son()
{
a = 20;
}
public:
int a;
};
void test()
{
son s;
cout << s.a << endl;
cout << sizeof(son) << endl;// Size is 8
// Access through the parent class name scope
cout<<s.Father::a << endl;
}2. When the subclass and parent have Function with the same name when , All function overloads of the parent class will also be hidden
And it can also be accessed through the scope
s.Father::func(10,20);Static member feature in inheritance
//1. Static members can be inherited
class Father
{
public:
static int mA;
static void func()
{
cout << "Father func()" << endl;
}
static void func(int a)
{
cout << "Father func(int a)" << endl;
}
static void func(int a, int b)
{
cout << "Father func(int a,int b)" << endl;
}
};
int Father::mA = 10;
class Son :public Father
{
public:
static int mA;
//static void func()
//{
// cout << "Son func()" << endl;
//}
//static int func()
//{
// cout << "int func()" << endl;
// return 0;
//}
};
int Son::mA = 20;
void test()
{
//2. Static member variables in inheritance will also be hidden by subclass member variables with the same name
Son s;
cout << s.mA << endl;
//cout << s.mB << endl;
//3. When a subclass and a parent class have static functions with the same name , All function overloads of the parent class will be hidden
s.Father::func(10);
//4.( Different from ordinary member functions ) In the inheritance , If you change a feature in a static function inherited from the base class in a subclass , Whether it's the return value , Or parameters , Will hide the functions overloaded by the base class
s.func(10);
//5.( Different from ordinary member functions ) A static member function cannot be a virtual function
//6. The static member variables inherited from the parent class are the static member variables of the parent class , Is the same
}1. Static members can be inherited .
2. Static member variables in inheritance will also be Homonymous Subclass Static member variable hide , if Static member variables with different names That is the static member variable of the parent class , Is the same .
3. When the subclass and parent have the same name Static functions when , All function overloads of the parent class will be hidden .
4.( Different from ordinary member functions ) In the inheritance , If you change a feature in a static function inherited from the base class in a subclass , Whether it's the return value , Or parameters , Will hide the functions overloaded by the base class .
5.( Different from ordinary member functions ) A static member function cannot be a virtual function .
Multiple inheritance
A class has more than two parent classes , When there is a member with the same name in the parent class, the subclass will produce ambiguity , I don't know which member to call , Not recommended , But the virtual inheritance method in diamond inheritance can solve this problem .
diamond inheritance

Don't diamond inheritance also have ambiguity ? How to solve it ?
use Virtual inheritance To solve
Virtual inheritance
Virtual inheritance requires virtual keyword , By virtual Keyword decorated inheritance is called virtual inheritance , The base class inherited by virtual is called virtual base class .
class Sheep : virtual public Animal
{
......
};The principle of virtual inheritance to solve ambiguity
Derived classes of virtual inheritance , The compiler will add a pointer to it , The pointer points to a table like organization , This table records the offset of the pointer from the variable ( This offset means , The offset of the first address of the class address from the variable ), And if you want to check this table , Can pass Look at the object model Look at , For example code :
class Animal
{
public:
Animal()
{
mA = 100;
}
public:
int mA;
};
class Sheep :virtual public Animal
{
};
class Camel :virtual public Animal
{
};
class SheepCamel :public Sheep, public Camel
{
};
be Sheep Object model of :

as well as SheepCamel Object model of

From the picture , Virtual inheritance can solve the problem of ambiguity , By virtual Modified class , The compiler generated a vbptr The pointer , This pointer points to the class and mA The offset distance of , Pay attention to this mA It is placed in a public area , such , When SheepCamel Subclasses also inherit Sheep and Camel When there are two parent classes , Does not inherit two parent classes at the same time mA Variable , It inherits two parent classes vbptr The pointer , The only one inherited mA Variables are from ancestor classes , therefore , In this way, there will be no ambiguity , You visited mA It's the ancestor mA.
In virtual inheritance, variables are accessed through pointers
Refer to the picture above , You can get variables through pointers
Sheep s;
s.mA;
//1.&s;// The first address of the class
//2.(int*)&s;// Forced to int* type ,int* Type is array type , Each byte is 4
//3.*(int*)&s;
Get the address in the pointer , Now if you +1, the reason being that int* type , In order to 4 An array of units ,
That is, divide the memory into bytes 4 Divide equally , Then bid 0,1,2,3... Divide , So you +1 In essence, I left 4 Bytes ,
Through your observation of the object model , You can also get mA Value , namely *((int*)(*(int*)(&s)) + 1)
//4.(int *)*(int*)&s;// Point to the first address of the table
//5.(int *)*(int*)&s+1;// Points to the address of the table storage offset
//6.*((int *)*(int*)&s+1);// This is the offset
cout << *((int*)*(int*)&s + 1) << endl;
//1.&s;
//2.(char*)&s;
char *p;*p For the string , The unit of bytes occupied is 1,p For the pointer , The unit of bytes occupied is 4
Here is the general &s Turn into char* type , It's a string type , So the unit of bytes is 1
//3.(char*)&s+*((int*)*(int*)&s + 1)// The occupied bytes are 1, Go to the first address, that is 0, Add the offset to get 4, This 4 Namely mA The address of .
//4. Convert type to Animal Pointer types
cout << ((Animal*)((char*)&s + *((int*)*(int*)&s + 1)))->mA << endl;The outputs are 4 and 100,4 It's the offset ,100 yes mA The value of the variable
Be careful (32 position ,vs):
1.Int*,char*,double*..... The size of pointer type in memory is always 4 byte
2.Int Occupy 4 byte ,char Occupy 1 byte ,double Occupy 8 byte ,float Occupy 4 byte
3.int a[n] The occupied bytes are 4*n
边栏推荐
- 【4.4 快速幂详解及快速幂求逆元】
- C and pointer Chapter 18 runtime environment 18.2 interface between C and assembly language
- Web middleware log analysis script 1.0 (shell script)
- 蓝桥杯 1004 [递归]母牛的故事
- [Qt]容器类、迭代器、foreach关键字
- 【3. Vim 操作】
- Transpose convolution correlation
- 类与对象笔记一
- Machine learning model -- lightgbm
- 知识蒸馏——pytorch实现
猜你喜欢

Deeplabcut uses 1

Knowledge distillation -- pytorch implementation

Deployment of yolov5 on Jetson nano deepstream

Signal and system impulse response and step response

放图仓库-Tsai

Transpose convolution correlation

Web middleware log analysis script 2.0 (shell script)

滑动窗口问题总结

Leetcode topic - array

9_逻辑回归(Logistic Regression)
随机推荐
12_决策树(Decision tree)
Recbole use 1
Huffman encoding and decoding
CSDN article syntax rules
Shufflenet series (2): explanation of shufflenet V2 theory
Request attribute in crawler
Torch. correlation function
卷积神经网络——LeNet(pytorch实现)
Machine learning model -- lightgbm
8_多项式回归及模型泛化(Polynomial Regression and Model Generalization)
【4.4 快速幂详解及快速幂求逆元】
C and pointer Chapter 18 runtime environment 18.1 judgment of runtime environment
7_主成分分析法(Principal Component Analysis)
C语言 求素数、闰年以及最小公倍数最大公约数
20220720 toss deeplobcut2
【AcWing第61场周赛】
动态联编和静态联编、以及多态
Signal and system impulse response and step response
Transpose convolution correlation
Leetcode topic - binary tree chapter