当前位置:网站首页>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 organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team

leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
![[微服务]分布式事务解决方案-Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[微服务]分布式事务解决方案-Seata

类和对象:中

机器学习初学者可以学哪些实战项目?

自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM

精心总结十三条建议,帮你创建更合适的MySQL索引

Notes on how to use zeno

MYSQL查询截取优化分析
随机推荐
Rasa 3.x Study Series - Rasa - Issues 4918 Study Notes
力扣2326、197
如何编辑epub电子书的目录
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
SC7A20 (Silan Micro-Accelerometer) Example
SC7A20(士兰微-加速度传感器)示例
vector的基本实现
Application of integrated stepper motor in UAV automatic airport
Fat interface in JQESAP system
Inheritance and friend, static member relationship
网关gateway跨域
Blueprint: Yang Hui's Triangular Arrangement
Notes on how to use zeno
Force buckle 2326, 197
ROS2系列知识(4): 理解【服务】的概念
欧拉系统(euleros):升级Mysql
sqlserver cannot connect remotely
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
Item 36: Specify std::launch::async if asynchronicity is essential.
leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]