C++继承中的对象构造与析构和赋值重载详解

目录

  • 一、构造/析构顺序及继承性
  • 二、拷贝构造的继承性
  • 三、赋值重载不具有继承性
  • 总结

一、构造/析构顺序及继承性
class A{private: int _a; public: A(int a = 0): _a(a) {cout << "A()" << this << endl; } ~A() {cout << "~A()"<< this <
C++继承中的对象构造与析构和赋值重载详解
文章图片

结论:
1.构造顺序:先构造基类,后构造派生类
2.析构顺序:先析构派生类,后析构基类

二、拷贝构造的继承性
class A{private: int _a; public: A(int a = 0): _a(a) {cout << "A()" << this << endl; } A(const A& src): _a(src._a) {cout << "A(const A& src)"<< this << endl; } ~A() {cout << "~A()"<< this <
C++继承中的对象构造与析构和赋值重载详解
文章图片

结论:
1.先调用基类缺省的构造函数,后调用派生类的拷贝构造函数
2.若派生类没有缺省构造函数A(),就会报错
疑惑:如何去调用基类的拷贝构造而不是缺省构造
#includeusing namespace std; class A{private: int _a; public: A(int a = 0) : _a(a) {cout << "A()" << this << endl; } A(const A& src) : _a(src._a) {cout << "A(const A& src)" << this << endl; } ~A() {cout << "~A()" << this << endl; }}; class B : public A{private: int _b; public: B(int b) : _b(b), A() {cout << "B()" << this << endl; } B(const B& src) : _b(src._b), A(src) //发生赋值兼容规则(切片) {cout << "B(const B& src)" << this << endl; } ~B() {cout << "~B()" << this << endl; }}; int main(){ B b(10); B b1(b); return 0; }

C++继承中的对象构造与析构和赋值重载详解
文章图片

结果:
将B类型src传递给A类型的A(const A& src)拷贝构造函数,发生了赋值兼容规则(切片现象)

三、赋值重载不具有继承性
#includeusing namespace std; class A{private: int _a; public: A(int a = 0) : _a(a) {cout << "A()" << this << endl; } A(const A& src) : _a(src._a) {cout << "A(const A& src)" << this << endl; } A& operator=(const A& src) {if(this != &src){_a = src._a; cout << "A& operator=(const A& src)" << endl; } } ~A() {cout << "~A()" << this << endl; }}; class B : public A{private: int _b; public: B(int b) : _b(b), A() {cout << "B()" << this << endl; } B(const B& src) : _b(src._b), A(src) //发生赋值兼容规则(切片) {cout << "B(const B& src)" << this << endl; } B& operator=(const B& src) {if(this != &src){_b = src._b; cout << "B& operator=(const B& src)" <
C++继承中的对象构造与析构和赋值重载详解
文章图片

结论:默认情况下仅仅调用了派生类的对象的赋值重载,并未调用基类的赋值重载。
【C++继承中的对象构造与析构和赋值重载详解】解决方案:
#includeusing namespace std; class A{private: int _a; public: A(int a = 0) : _a(a) {cout << "A()" << this << endl; } A(const A& src) : _a(src._a) {cout << "A(const A& src)" << this << endl; } A& operator=(const A& src) {if(this != &src){_a = src._a; cout << "A& operator=(const A& src)" << endl; } } ~A() {cout << "~A()" << this << endl; }}; class B : public A{private: int _b; public: B(int b) : _b(b), A() {cout << "B()" << this << endl; } B(const B& src) : _b(src._b), A(src) //发生赋值兼容规则(切片) {cout << "B(const B& src)" << this << endl; } B& operator=(const B& src) {if(this != &src){*(A*)this = src; //将调用基类赋值重载_b = src._b; cout << "B& operator=(const B& src)" <
C++继承中的对象构造与析构和赋值重载详解
文章图片


总结 本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

    推荐阅读