C++11中delete和default的用法詳解
1 特殊成員函數(shù)
一個(gè)類,當(dāng)只有數(shù)據(jù)成員時(shí),C++98 編譯器會(huì)隱式的產(chǎn)生四個(gè)函數(shù):缺省構(gòu)造函數(shù),析構(gòu)函數(shù),拷貝構(gòu)造函數(shù) 和 拷貝賦值算子,稱為特殊成員函數(shù)
class DataOnly {
private:
int data_;
}; C++11 中,還有額外的兩個(gè)特殊成員函數(shù):移動(dòng)構(gòu)造函數(shù) 和 移動(dòng)賦值算子
class DataOnly {
public:
DataOnly () // default constructor
~DataOnly () // destructor
DataOnly (const DataOnly & rhs) // copy constructor
DataOnly & operator=(const DataOnly & rhs) // copy assignment operator
DataOnly (const DataOnly && rhs) // C++11, move constructor
DataOnly & operator=(DataOnly && rhs) // C++11, move assignment operator
}; 2 禁止編譯器合成函數(shù)
作為開(kāi)發(fā)者,如果不想讓用戶使用某個(gè)成員函數(shù),不聲明即可;但對(duì)于特殊成員函數(shù),則是另一種情況
例如,設(shè)計(jì)一個(gè)樹(shù)葉類
class LeafOfTree{
// ...
}; 萊布尼茨說(shuō)過(guò),"世上沒(méi)有兩片完全相同的樹(shù)葉" (Es gibt keine zwei Blätter, die gleich bleiben),因此,對(duì)于一片獨(dú)一無(wú)二的樹(shù)葉,下面的操作是錯(cuò)誤的:
LeafOfTree leaf1; LeafOfTree leaf2; LeafOfTree leaf3(leaf1); // attempt to copy Leaf1 — should not compile! Leaf1 = Leaf2; // attempt to copy Leaf2 — should not compile!
此時(shí),需要避免使用 "拷貝構(gòu)造函數(shù)" 和 "拷貝賦值算子"
2.1 私有+不實(shí)現(xiàn)
C++98 中,可聲明這些特殊成員函數(shù)為私有型 (private),且不實(shí)現(xiàn)該函數(shù),具體如下:
class LeafOfTree{
private:
LeafOfTree( const LeafOfTree& ); // not defined
LeafOfTree & operator=( const LeafOfTree& ); // not defined
}; 程序中如果調(diào)用了 LeafOfTree 類的拷貝構(gòu)造函數(shù) (或拷貝賦值操作符),則在編譯時(shí),會(huì)出現(xiàn)鏈接錯(cuò)誤 (link-time error)
為了將報(bào)錯(cuò)提前到編譯時(shí) (compile time),可增加一個(gè)基類 Uncopyable,并將拷貝構(gòu)造函數(shù)和拷貝賦值算子聲明為私有型,具體可參見(jiàn) 《Effective C++》3rd item 6
在較早的谷歌 C++ 編碼規(guī)范中,使用了一個(gè)宏來(lái)簡(jiǎn)化,如下:
// A macro to disallow the copy constructor and operator= functions // This should be used in the priavte:declarations for a class #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ TypeName& operator=(const TypeName&)
2.2 delete 關(guān)鍵字
C++11 中,可在 "禁止使用" 的特殊成員函數(shù)聲明后加 "= delete",而需要保留的加 "= default" 或不采取操作,能夠達(dá)到"私有+不實(shí)現(xiàn)"的同等效果
class LeafOfTree{
public:
LeafOfTree() = default;
~LeafOfTree() = default;
LeafOfTree(const LeafOfTree&) = delete; // mark copy ctor or copy assignment operator as deleted functions
LeafOfTree & operator=(const LeafOfTree&) = delete;
}; 3 delete 的擴(kuò)展
C++11 中,delete 關(guān)鍵字可用于任何函數(shù),不僅僅局限于類成員函數(shù)
3.1 函數(shù)重載
在函數(shù)重載中,可用 delete 來(lái)濾掉一些函數(shù)的形參類型,如下:
bool IsLucky(int number); // original function bool IsLucky(char) = delete; // reject chars bool IsLucky(bool) = delete; // reject bools bool IsLucky(double) = delete; // reject doubles and floats
這樣在調(diào)用 IsLucky 函數(shù)時(shí),如果參數(shù)類型不對(duì),則會(huì)出現(xiàn)錯(cuò)誤提示
if (IsLucky('a')) … // error ! call to deleted function
if (IsLucky(true)) … // error !
if (IsLucky(3.5)) … // error ! 3.2 模板特例化
在模板特例化中,也可以用 delete 來(lái)過(guò)濾一些特定的形參類型。
例如,Widget 類中聲明了一個(gè)模板函數(shù),當(dāng)進(jìn)行模板特化時(shí),要求禁止參數(shù)為 void* 的函數(shù)調(diào)用。
如果按照 C++98 的 "私有+不實(shí)現(xiàn)" 思路,應(yīng)該是將特例化的函數(shù)聲明為私有型,如下:
class Widget {
public:
template<typename T>
void ProcessPointer(T* ptr) { … }
private:
template<>
void ProcessPointer<void>(void*); // error!
}; 問(wèn)題是,模板特例化應(yīng)當(dāng)被寫(xiě)在命名空間域 (namespace scope),而不是類域 (class scope),因此,該方法會(huì)報(bào)錯(cuò)。
而 C++11 中,因?yàn)橛?delete 關(guān)鍵字,則可直接在類域外,將特例化的模板函數(shù)聲明為 delete, 如下:
class Widget {
public:
template<typename T>
void ProcessPointer(T* ptr) { … }
};
template<>
void Widget::ProcessPointer<void>(void*) = delete; // still public, but deleted 這樣,當(dāng)程序代碼中,有調(diào)用 void* 作形參的 ProcessPointer 函數(shù)時(shí),編譯時(shí)就會(huì)報(bào)錯(cuò)。
小結(jié)
C++98 中這種 "私有+不實(shí)現(xiàn)"的方式,其實(shí)是模仿 C++11 中的 delete 功能,本身有一些局限:在類外不起作用;在類內(nèi)有時(shí)不起作用;有時(shí)可能直到鏈接時(shí)才會(huì)起作用。總之,沒(méi)有 delete 好用。
因此建議:
- Prefer deleted functions to private undefined ones
- Any function may be deleted, including non-member functions and template instantiations
到此這篇關(guān)于C++11中delete和default的用法詳解的文章就介紹到這了,更多相關(guān)C++ delete default內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)鄰接表頂點(diǎn)的刪除
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)鄰接表頂點(diǎn)的刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C語(yǔ)言中多維數(shù)組的內(nèi)存分配和釋放(malloc與free)的方法
寫(xiě)代碼的時(shí)候會(huì)碰到多維數(shù)組的內(nèi)存分配和釋放問(wèn)題,在分配和釋放過(guò)程中很容易出現(xiàn)錯(cuò)誤。下面貼上一些示例代碼,以供參考。2013-05-05
QT使用SQLite數(shù)據(jù)庫(kù)超詳細(xì)教程(增刪改查、對(duì)大量數(shù)據(jù)快速存儲(chǔ)和更新)
這篇文章主要給大家介紹了關(guān)于QT使用SQLite數(shù)據(jù)庫(kù)的相關(guān)資料,其中包括增刪改查以及對(duì)大量數(shù)據(jù)快速存儲(chǔ)和更新,SQLite是一種嵌入式關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它是一個(gè)軟件庫(kù),提供了一個(gè)自包含、無(wú)服務(wù)器、零配置的、事務(wù)性的SQL數(shù)據(jù)庫(kù)引擎,需要的朋友可以參考下2024-01-01
C語(yǔ)言實(shí)現(xiàn)自動(dòng)售貨機(jī)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)自動(dòng)售貨機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C++函數(shù)的嵌套調(diào)用和遞歸調(diào)用學(xué)習(xí)教程
這篇文章主要介紹了C++函數(shù)的嵌套調(diào)用和遞歸調(diào)用學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C++ 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列
這篇文章主要介紹了詳解C++ 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列的相關(guān)資料,需要的朋友可以參考下2017-03-03
VC++實(shí)現(xiàn)CStdioFile寫(xiě)入及讀取文件并自動(dòng)換行的方法
這篇文章主要介紹了VC++實(shí)現(xiàn)CStdioFile寫(xiě)入及讀取文件并自動(dòng)換行的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08

