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

