C++踩坑實戰(zhàn)之構(gòu)造和析構(gòu)函數(shù)
前言
我是練習(xí)時長一年的 C++ 個人練習(xí)生,喜歡野指針、模板報錯和未定義行為(undefined behavior)。之前在寫設(shè)計模式的『工廠模式』時,一腳踩到了構(gòu)造、繼承和 new 組合起來的坑,現(xiàn)在也有時間來整理一下了。
構(gòu)造函數(shù)
眾所周知:在創(chuàng)建對象時,防止有些成員沒有被初始化導(dǎo)致不必要的錯誤,在創(chuàng)建對象的時候自動調(diào)用構(gòu)造函數(shù)(無聲明類型),完成成員的初始化。即:
Class c // 隱式,默認(rèn)構(gòu)造函數(shù)
Class c = Class() // 顯示,默認(rèn)構(gòu)造函數(shù)
Class c = Class("name") // 顯示,非默認(rèn)構(gòu)造函數(shù)
Class* c = new Class // 隱式,默認(rèn)構(gòu)造函數(shù)
- 構(gòu)造函數(shù)執(zhí)行前,對象不存在
- 構(gòu)造函數(shù)創(chuàng)建對象后,對象不能調(diào)用構(gòu)造函數(shù)
- 類中如果不定義構(gòu)造函數(shù),編譯器提供有默認(rèn)的構(gòu)造函數(shù),無參數(shù),也不執(zhí)行任何額外的語句
- 如果提供非默認(rèn)構(gòu)造函數(shù),沒有默認(rèn)構(gòu)造函數(shù)將會出錯。所以要定義一個不接受任何參數(shù)的構(gòu)造函數(shù),并為成員定義合理的值
- 一般而言,默認(rèn)的構(gòu)造函數(shù)是用來對所有類成員做隱式初始化的
- 自己定義的構(gòu)造函數(shù)一般用使用列表初始化來初始化參數(shù)
- 通過構(gòu)造函數(shù)對成員賦值,要優(yōu)于通過函數(shù)為成員賦值
using namespace std;
class Stone {
private:
int weight{0};
double radius{0.0};
public:
Stone() {
cout << "Class Stone was created by default creator" << endl;
};
Stone(int w, double r) : weight{w}, radius{r} {
cout << "Class Stone was created by custom creator" << endl;
}
void showInfo() {
cout << "Weight: " << this->weight << ", Radius: "
<< this->radius << endl;
}
};
int main (){
// 隱式,成員有默認(rèn)值
Stone s1;
s1.showInfo();
// 顯式,通過列表初始化,為成員賦值
Stone s2 = Stone(12, 3.3);
s2.showInfo();
return 0;
}
通過構(gòu)造函數(shù)實現(xiàn)的類型轉(zhuǎn)換
觀察以下的代碼,我們發(fā)現(xiàn) Stone s2;s2 = 3.3; 這樣將一個 double 類型的數(shù)據(jù)賦值給類類型并沒有出錯,這是隱式類型轉(zhuǎn)換,從參數(shù)類型到類類型。
using namespace std;
class Stone {
private:
int weight{0};
double radius{0.0};
public:
Stone() {
cout << this << endl;
cout << "Class Stone was created by default creator" << endl;
};
// 都關(guān)閉
Stone(double r) : radius{r} {
cout << this << endl;
cout << "Class Stone was created by parameter radius" << endl;
}
Stone(int w) : weight{w} {
cout << this << endl;
cout << "Class Stone was created by parameter weight" << endl;
}
void showInfo() {
cout << "Weight: " << this->weight << ", Radius: "
<< this->radius << endl;
}
};
int main (){
Stone s2;
s2 = 3.3;
s2.showInfo();
return 0;
}
這是因為:接受一個參數(shù)的構(gòu)造函數(shù)允許使用賦值語法來為對象賦值。s2=3.3 會創(chuàng)建 Stock(double) 臨時對象,臨時對象初始化后,逐成員賦值的方式復(fù)制到對象中,在幾個構(gòu)造函數(shù)中加入了 cout << this 的語句,由對象的地址不同,可以判斷該賦值語句額外生成了臨時對象。
為了防止隱式轉(zhuǎn)換帶來的危險,可以使用關(guān)鍵字 explicit 關(guān)閉這一特性,這樣就得顯式完成參數(shù)類型到類類型的轉(zhuǎn)換:s = Stock(1.3);不過,得保證沒有二義性。
using namespace std;
class Stone {
private:
int weight{0};
double radius{0.0};
public:
Stone() {
cout << this << endl;
cout << "Class Stone was created by default creator" << endl;
};
// 都關(guān)閉
explicit Stone(double r) : radius{r} {
cout << this << endl;
cout << "Class Stone was created by parameter radius" << endl;
}
explicit Stone(int w) : weight{w} {
cout << this << endl;
cout << "Class Stone was created by parameter weight" << endl;
}
void showInfo() {
cout << "Weight: " << this->weight << ", Radius: "
<< this->radius << endl;
}
};
int main (){
Stone s2;
s2 = Stone(3);
s2.showInfo();
return 0;
}
上述代碼中,如果 Stone(int w) 沒有被關(guān)閉,那么 s2=3.3 將調(diào)用這一構(gòu)造函數(shù)。所以構(gòu)造函數(shù)建議都加上 explicit 聲明。
派生類的構(gòu)造函數(shù)
派生類要注意的是:派生類被構(gòu)造之前,通過調(diào)用一個基類的構(gòu)造函數(shù),創(chuàng)建基類完成基類數(shù)據(jù)成員的初始化;也就是說,基類對象在程序進入派生類構(gòu)造函數(shù)之前被創(chuàng)建。那么,可以通過初始化列表傳遞給基類參數(shù),不傳遞的話,調(diào)用基類的默認(rèn)的構(gòu)造函數(shù),如下述程序中的:Gem(){}:Stone()。
using namespace std;
class Stone {
private:
int weight{0};
double radius{0.0};
public:
Stone() {
cout << "This object was in address: " << this << endl;
};
Stone(int w, double r) : weight{2}, radius{r} {};
void showInfo() {
cout << "Weight: " << this->weight << ", Radius: " << this->radius;
}
int getWeight(){
return this->weight;
}
auto getRadius() -> double {
return this->radius;
}
};
class Gem : public Stone {
private:
double price;
public:
Gem(){};
Gem(double p, int w, double r) : Stone(w, r), price{p} {};
void show() {
cout << "Weight: " << this->getWeight() << ", Radius"
<< this->getRadius();
}
};
int main (){
Gem g1; // call default
Gem g2 = Gem(1300, 1, 2.3); // call custom
// g.setWeight(130);
g2.show();
return 0;
}
- 首先創(chuàng)建基類對象
- 派生類通過初始化列表(只能用在構(gòu)造函數(shù))將基類信息傳遞給基類的構(gòu)造函數(shù)
- 派生類構(gòu)造函數(shù)可以為派生類初始化新的成員
析構(gòu)函數(shù)
對象過期時,程序會調(diào)用對象的析構(gòu)函數(shù)完成一些清理工作,如釋放變量開辟的空間等。如構(gòu)造函數(shù)使用了 new 來申請空間,析構(gòu)就需要 delete 來釋放空間。如果沒有特別聲明析構(gòu)函數(shù),編譯器會為類提供默認(rèn)的析構(gòu)函數(shù),在對象作用域到期、被刪除時自動被調(diào)用。
如 stock1 = Stock(),這種就申請了一個臨時變量,變量消失時會調(diào)用析構(gòu)函數(shù)。此外,這種局部變量放在棧區(qū),先入后出,也就是,最后被申請的變量最先被釋放。
using namespace std;
class Stone {
private:
int weight{0};
double radius{0.0};
public:
Stone() {
cout << "This object was in address: " << this << endl;
};
~Stone() {
cout << this << " Object was deleted." << endl;
}
};
int main (){
{
Stone s1;
Stone s2;
}
return 0;
}
繼承中的析構(gòu)函數(shù)
繼承類比較容易理解,畢竟都學(xué)過面向?qū)ο?。公有繼承的時候,基類的公有成員也是派生類的共有成員;私有成員也是派生類的一部分,不過需要共有或保護方法來訪問。但是但是但是,派生類和基類的析構(gòu)函數(shù)之間,也是一個坑。在繼承中:
- 如果一個方法不是虛方法,那么將根據(jù)引用類型或指針類型選擇執(zhí)行的方法
- 如果一個方法是虛方法,將根據(jù)指針或引用指向?qū)ο蟮念愋瓦x擇執(zhí)行的方法
在繼承中,對象的銷毀順序和創(chuàng)建相反。創(chuàng)建時先創(chuàng)建基類,而后創(chuàng)建子類;銷毀時,先調(diào)用子類的析構(gòu)函數(shù),而后自動調(diào)用基類的析構(gòu)函數(shù)。因此,對于基類而言,建議將析構(gòu)函數(shù)寫成虛方法。如果析構(gòu)不是虛方法,對于以下情況,只有基類的析構(gòu)被調(diào)用;如果析構(gòu)是虛方法,子類、基類的析構(gòu)方法都被調(diào)用。可以嘗試刪除下述代碼的 virtual 來觀察結(jié)果:
using namespace std;
class Stone {
private:
int weight{0};
double radius{0.0};
public:
Stone() {
cout << "This object was in address: " << this << endl;
};
Stone(int w, double r) : weight{2}, radius{r} {};
void showInfo() {
cout << "Weight: " << this->weight << ", Radius: "
<< this->radius;
}
int getWeight(){
return this->weight;
}
auto getRadius() -> double {
return this->radius;
}
virtual ~Stone() {
cout << "Stone class was deleted." << endl;
}
};
class Gem : public Stone {
private:
double price;
public:
Gem() {};
Gem(double p, int w, double r) : Stone(w, r), price{p} {};
void show() {
cout << "Weight: " << this->getWeight() << ", Radius"
<< this->getRadius();
}
~Gem() {
cout << "Gem class was deleted." << endl;
}
};
int main (){
Stone* s1 = new Gem(2.3, 2, 3.2);
delete s1;
// Gem* g1 = new Gem(2.3, 2, 1.2);
// delete g1;
return 0;
}
應(yīng)用
大概常見的坑在上面都記錄好了,來看一段我寫的危險的程序(我大概抽象了一下),覆蓋了:野指針和為定義行為:
using namespace std;
class A {
private:
int* a;
public:
int* create() {
a = new int();
return a;
}
~A(){
delete a;
}
};
int main () {
A a;
int* b = a.create();
delete b;
return 0;
}
- 每次調(diào)用 create 都會 new 一次,但只 delete 了一次。
- 如果沒有調(diào)用 create 直接析構(gòu),未定義行為
- 如果 b 持有了 a.create() 的指針,然后 a 提前析構(gòu),那么 b 是野指針
- delete b 是沒必要的。這樣會 double free,也是未定義行為
- 上述代碼沒有區(qū)分類里面 new 且 返回的東西要在哪刪除合適
- 可以讓類來管理這一個 new,修改一下 create 的實現(xiàn)或者干脆在構(gòu)造 new,在析構(gòu) delete
總結(jié)
到此這篇關(guān)于C++踩坑實戰(zhàn)之構(gòu)造和析構(gòu)函數(shù)的文章就介紹到這了,更多相關(guān)C++構(gòu)造和析構(gòu)函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tinyxml 常用的C++ XML解析器非常優(yōu)秀
讀取和設(shè)置xml配置文件是最常用的操作,試用了幾個C++的XML解析器,個人感覺TinyXML是使用起來最舒服的,因為它的API接口和Java的十分類似,面向?qū)ο笮院芎?/div> 2012-11-11
C語言驅(qū)動開發(fā)之通過ReadFile與內(nèi)核層通信
驅(qū)動與應(yīng)用程序的通信是非常有必要的,內(nèi)核中執(zhí)行代碼后需要將其動態(tài)顯示給應(yīng)用層。為了實現(xiàn)內(nèi)核與應(yīng)用層數(shù)據(jù)交互則必須有通信的方法,微軟為我們提供了三種通信方式,本文先來介紹通過ReadFile系列函數(shù)實現(xiàn)的通信模式2022-09-09
C和C++中的基本數(shù)據(jù)類型的大小及表示范圍詳解
這篇文章主要介紹了C和C++中的基本數(shù)據(jù)類型的大小及表示范圍詳解,基本數(shù)據(jù)類型有int、long、long long、float、double、char、string,正文有詳細(xì)介紹,歡迎參考2018-01-01最新評論

