C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載詳解
一、構(gòu)造/析構(gòu)順序及繼承性
class A
{
private:
int _a;
public:
A(int a = 0): _a(a)
{
cout << "A()" << 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()
{
cout << "~B()"<< this <<endl;
}
};

結(jié)論:
1.構(gòu)造順序:先構(gòu)造基類,后構(gòu)造派生類
2.析構(gòu)順序:先析構(gòu)派生類,后析構(gòu)基類
二、拷貝構(gòu)造的繼承性
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)
{
cout << "B(const B& src)" << this << endl;
}
~B()
{
cout << "~B()"<< this <<endl;
}
};

結(jié)論:
1.先調(diào)用基類缺省的構(gòu)造函數(shù),后調(diào)用派生類的拷貝構(gòu)造函數(shù)
2.若派生類沒(méi)有缺省構(gòu)造函數(shù)A(),就會(huì)報(bào)錯(cuò)
疑惑:如何去調(diào)用基類的拷貝構(gòu)造而不是缺省構(gòu)造
#include<iostream>
using 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) //發(fā)生賦值兼容規(guī)則(切片)
{
cout << "B(const B& src)" << this << endl;
}
~B()
{
cout << "~B()" << this << endl;
}
};
int main()
{
B b(10);
B b1(b);
return 0;
}

結(jié)果:
將B類型src傳遞給A類型的A(const A& src)拷貝構(gòu)造函數(shù),發(fā)生了賦值兼容規(guī)則(切片現(xiàn)象)
三、賦值重載不具有繼承性
#include<iostream>
using 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) //發(fā)生賦值兼容規(guī)則(切片)
{
cout << "B(const B& src)" << this << endl;
}
B& operator=(const B& src)
{
if(this != &src)
{
_b = src._b;
cout << "B& operator=(const B& src)" << endl;
}
}
~B()
{
cout << "~B()" << this << endl;
}
};
int main()
{
B b1(10);
B b2(20);
b1 = b2;
return 0;
}

結(jié)論:默認(rèn)情況下僅僅調(diào)用了派生類的對(duì)象的賦值重載,并未調(diào)用基類的賦值重載。
解決方案:
#include<iostream>
using 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) //發(fā)生賦值兼容規(guī)則(切片)
{
cout << "B(const B& src)" << this << endl;
}
B& operator=(const B& src)
{
if(this != &src)
{
*(A*)this = src; //將調(diào)用基類賦值重載
_b = src._b;
cout << "B& operator=(const B& src)" << endl;
}
}
~B()
{
cout << "~B()" << this << endl;
}
};
int main()
{
B b1(10);
B b2(20);
b1 = b2;
return 0;
}

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- C++構(gòu)造析構(gòu)賦值運(yùn)算函數(shù)應(yīng)用詳解
- C++淺析構(gòu)造函數(shù)的特性
- C++類與對(duì)象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
- C++超詳細(xì)講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實(shí)現(xiàn)
- C++分析類的對(duì)象作類成員調(diào)用構(gòu)造與析構(gòu)函數(shù)及靜態(tài)成員
- C++分析構(gòu)造函數(shù)與析造函數(shù)的特點(diǎn)梳理
- 一起來(lái)學(xué)習(xí)C++的構(gòu)造和析構(gòu)
- C++編程析構(gòu)函數(shù)拷貝構(gòu)造函數(shù)使用示例詳解
- C++類的構(gòu)造與析構(gòu)特點(diǎn)及作用詳解
相關(guān)文章
用C語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛擬機(jī)
這篇文章主要介紹了用C語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛擬機(jī),其中棧數(shù)組的部分非常值得學(xué)習(xí),需要的朋友可以參考下2015-07-07
C++實(shí)現(xiàn)LeetCode(161.一個(gè)編輯距離)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(161.一個(gè)編輯距離),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言中system()執(zhí)行cmd命令打開(kāi)關(guān)閉程序的方法
今天小編就為大家分享一篇C語(yǔ)言中system()執(zhí)行cmd命令打開(kāi)關(guān)閉程序的方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
C語(yǔ)言設(shè)計(jì)圖書(shū)登記系統(tǒng)與停車場(chǎng)管理系統(tǒng)的實(shí)例分享
這篇文章主要介紹了C語(yǔ)言設(shè)計(jì)圖書(shū)登記系統(tǒng)與停車場(chǎng)管理系統(tǒng)的實(shí)例分享,重在以最簡(jiǎn)單的一些需求來(lái)展示管理系統(tǒng)的設(shè)計(jì)思路,需要的朋友可以參考下2016-06-06
C++ using namespace std 用法深入解析
以下是對(duì)C++中using namespace std的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07

