C++繼承中的對象構(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.若派生類沒有缺省構(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)用了派生類的對象的賦值重載,并未調(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é)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- C++構(gòu)造析構(gòu)賦值運(yùn)算函數(shù)應(yīng)用詳解
- C++淺析構(gòu)造函數(shù)的特性
- C++類與對象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
- C++超詳細(xì)講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實(shí)現(xiàn)
- C++分析類的對象作類成員調(diào)用構(gòu)造與析構(gòu)函數(shù)及靜態(tài)成員
- C++分析構(gòu)造函數(shù)與析造函數(shù)的特點(diǎn)梳理
- 一起來學(xué)習(xí)C++的構(gòu)造和析構(gòu)
- C++編程析構(gòu)函數(shù)拷貝構(gòu)造函數(shù)使用示例詳解
- C++類的構(gòu)造與析構(gòu)特點(diǎn)及作用詳解
相關(guān)文章
Visual Studio Community 2022(VS2022)安裝圖文方法
這篇文章主要介紹了Visual Studio Community 2022(VS2022)安裝方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
C語言報(bào)錯(cuò)Use of Uninitialized Variable的原因及解決方案
Use of Uninitialized Variable是C語言中常見且危險(xiǎn)的錯(cuò)誤之一,它通常在程序試圖使用一個(gè)未初始化的變量時(shí)發(fā)生,本文將詳細(xì)介紹Use of Uninitialized Variable的產(chǎn)生原因,提供多種解決方案,并通過實(shí)例代碼演示如何有效避免和解決此類錯(cuò)誤,需要的朋友可以參考下2024-06-06
使用DeepSeek API 結(jié)合VSCode提升開發(fā)效率
這篇文章主要介紹了DeepSeek API與Visual Studio Code (VSCode)結(jié)合使用,以提升軟件開發(fā)效率,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
關(guān)于C++STL string類的介紹及模擬實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于C++STL string類的介紹及模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下面具體的文章內(nèi)容2021-09-09
epoll多路復(fù)用的一個(gè)實(shí)例程序(C實(shí)現(xiàn))
這篇文章主要為大家詳細(xì)介紹了epoll多路復(fù)用的一個(gè)實(shí)例程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C++實(shí)現(xiàn)打印虛函數(shù)表的地址
對于存在虛函數(shù)的類,如何打印虛函數(shù)表的地址,并利用這個(gè)虛函數(shù)表的地址來執(zhí)行該類中的虛函數(shù)呢,下面小編就來和大家一起簡單聊聊吧2023-07-07
QT實(shí)戰(zhàn)之實(shí)現(xiàn)圖片瀏覽系統(tǒng)
這篇文章主要介紹了如何利用QT編寫一個(gè)圖片瀏覽系統(tǒng),可以支持自動(dòng)播放,左右拖動(dòng)切換,點(diǎn)擊列表切換,點(diǎn)擊按鈕切換等功能,感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04
C++開發(fā):為什么多線程讀寫shared_ptr要加鎖的詳細(xì)介紹
本篇文章介紹了,在C++中為什么多線程讀寫shared_ptr要加鎖的詳細(xì)說明。需要的朋友參考下2013-04-04

