当前位置:网站首页>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 fatherThe 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;}\
边栏推荐
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
- vector的基本实现
- Fat interface in JQESAP system
- OSF understands the agile development model in one minute
- MYSQL事务
- Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
- Key Points Estimation and Point Instance
- Exam preparation plan
- gateway gateway cross domain
- RTL8762DK uses DebugAnalyzer (four)
猜你喜欢

MYSQL索引解析

Classes and Objects: Medium

RTL8762DK UART (two)

Modern Enterprise Architecture Framework 1

LeetCode每日一练 —— 环形链表问题(面试四连问)

RTL8762DK 点灯/LED(三)

Four ways the Metaverse is changing the way humans work

RTL8762DK WDG (six)

Beijing suddenly announced that yuan universe big news

pycaret source code analysis: download dataset\Lib\site-packages\pycaret\datasets.py
随机推荐
JS时间戳的意义是什么?知道sql会考虑加时间戳,JS的时间戳用于什么场景?
Unity3D学习笔记10——纹理数组
现代企业架构框架1
力扣二叉树
C string array reverse
RTL8762DK PWM (seven)
WebApi hits an Attribute to handle exceptions uniformly
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
MVCC总结
RTL8762DK WDG(六)
The principle of virtual inheritance
leetcode:1648. 销售价值减少的颜色球【二分找边界】
Introduction to machine learning how to?
MYSQL关键字Explain解析
Detailed explanation of TCP protocol
leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
Nmap Operation Manual - Full Version
Matlab / Arcgis处理nc数据
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
Fat interface in JQESAP system