当前位置:网站首页>Inheritance Considerations
Inheritance Considerations
2022-08-01 01:10:00 【HUAWEI CLOUD】
The destructor of the subclass will automatically call the destructor of the superclass to clean up the members of the superclass after being called.Because this can ensure the order in which the subclass members are cleaned up first and then the superclass members are cleaned up.
- Because of the reason behind polymorphism,The destructor name of any class is uniformly handled as :
destructor()
,So the compiler will think that the destructor of the subclass and the destructor of the parent class constitute a hidden relationship - 为了保证析构时:保持
Son before father
The LIFO sequential destructor,子类析构函数完成后,会自动调用父类的析构函数
初始化时:When we first call the function of the parent class to initialize the part of the parent class,Then initialize the subclass part, To conform to the characteristics of the stack:后进先出
所以析构的时候:Destruct the subclass first,Then destruct the parent class
//派生类的析构函数~Student(){ cout << "~Student()" << endl;}
总结:
//派生类class Student : public Person{public: //派生类的构造函数 Student(const char* name = "Mango", int id = 0) :Person(name) //Treat the parent class as a whole,显示的初始化 调用基类的构造函数初始化基类的那一部分成员 ,_id(id)//初始化派生类的成员 { //Call the parent class constructor to initialize the part of the inherited parent class //Then initialize the derived class's own members cout << "Student()" << endl; } //派生类的拷贝构造 Student(const Student& s) :Person(s) //Pass the subclass a reference to the superclass,is a slicing behavior 调用基类的拷贝构造函数完成基类成员的拷贝构造 ,_id(s._id)//拷贝构造派生类的成员 { //Calling the parent class copy constructor is used to copy construct the inherited part of the parent class //Then copy-construct the derived class's own members cout << "Student(const Student& s)" << endl; } //派生类的operator=赋值 Student& operator=(const Student& s) { cout << "Student& operator=(const Student& s)" << endl; //防止自己给自己赋值 if (this != &s) { Person::operator=(s);//调用基类的operator=完成基类成员的赋值 _id = s._id;//完成派生类成员的赋值 } return *this; } //派生类的析构函数 ~Student() { cout << "~Student()" << endl; //派生类的析构函数调用完成后,会自动调用基类的析构函数 }private: int _id;};
注意:
派生类对象初始化先调用基类构造再调派生类构造. (初始化:先父再子)
派生类对象析构清理先调用派生类析构再调基类的析构.(析构:Son before father)
- 派生类和基类的赋值运算符(operator=)Overloaded functions are hidden because the function names are the same,因此在派生类当中调用基类的赋值运算符重载函数时,需要使用The scope qualifier makes the specified call.
- 由于多态的某些原因,任何类的析构函数名都会被统一处理为
destructor();
.因此,派生类和基类的Destructors are also hidden due to the same function name,若是我们The base class's destructor needs to be called somewhere,那么就要使用The scope qualifier makes the specified call. - 在派生类的拷贝构造函数和
operator=
当中调用基类的拷贝构造函数和operator=
的传参方式是一个切片行为,都是将派生类对象直接赋值给基类的引用. - 基类的构造函数、拷贝构造函数、赋值运算符重载函数我们都可以在派生类当中自行进行调用,而基类的析构函数是当派生类的析构函数被调用后由编译器自动调用的,我们若是自行调用基类的构造函数就会导致基类被析构多次的问题.
- 创建派生类对象时是先创建的基类成员再创建的派生类成员,编译器为了保证析构时先析构派生类成员再析构基类成员的顺序析构,所以编译器会在派生类的析构函数被调用后自动调用基类的析构函数.
题目:设计出一个类A,让这个类不能被继承(Inherited is useless)
想法:只需让AThe default constructor is private
Because derived classes want to create objects,派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员.And if the constructor of the base class is not accessible,就不能创建对象.
class A{private: A() {}};class B :public A{};int main(){ B b;//报错,No error will be reported if the object is not created return 0;}
\
边栏推荐
- 数据中台建设(七):数据资产管理
- Detailed explanation of TCP protocol
- Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
- ROS2 series of knowledge (4): understand the concept of [service]
- WeChat applet page syntax
- Rasa 3.x Study Series - Rasa - Issues 4918 Study Notes
- MVCC总结
- Js replication
- The principle of virtual inheritance
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
猜你喜欢
ROS2 series of knowledge (4): understand the concept of [service]
精心总结十三条建议,帮你创建更合适的MySQL索引
Redis五种数据类型简介
SC7A20 (Silan Micro-Accelerometer) Example
What practical projects can machine learning beginners learn?
Rainbow share | how to use moving targets defense technology to guard against the unknown
机器学习初学者可以学哪些实战项目?
Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
JVM面试题总结(持续更新中)
SC7A20(士兰微-加速度传感器)示例
随机推荐
二叉树遍历非递归程序 -- 使用栈模拟系统栈
Js replication
RTL8762DK PWM(七)
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
Key Points Estimation and Point Instance
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
OSF一分钟了解敏捷开发模式
精心总结十三条建议,帮你创建更合适的MySQL索引
Rasa 3.x Study Series - Rasa - Issues 4918 Study Notes
MYSQL查询截取优化分析
设计消息队列存储消息数据的MySQL表格
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
Introduction to the decision logic of WAASAP WebClient UI page labels
vector的基本实现
现代企业架构框架1
how to edit the table of contents of an epub ebook
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle
数据中台建设(七):数据资产管理
gateway gateway cross domain