您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載實(shí)例分析”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載實(shí)例分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
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)基類
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.若派生類沒有缺省構(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; }
讀到這里,這篇“C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載實(shí)例分析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。