1 特殊成員函數
一個類,只有數據成員時
class DataOnly { private: int data_; };
C++98 編譯器會隱式的產生四個函數:缺省構造函數,析構函數,拷貝構造函數 和 拷貝賦值算子,它們稱為特殊成員函數 (special member function)
在 C++11 中,除了上面四個外,特殊成員函數還有兩個:移動構造函數 和 移動賦值算子
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 禁止編譯器合成函數
作為開發方,如果不想讓用戶使用某個成員函數,不聲明即可;但對於特殊成員函數,則是另一種情況。例如,設計一個樹葉類:
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!
因此,此時需要避免使用 “拷貝構造函數” 和 “拷貝賦值算子”
2.1 私有+不實現
C++98 中,可聲明這些特殊成員函數為私有型 (private),且不實現該函數,具體如下:
class LeafOfTree{ private: LeafOfTree( const LeafOfTree& ); // not defined LeafOfTree & operator=( const LeafOfTree& ); // not defined };
程序中如果調用了 LeafOfTree 類的拷貝構造函數 (或拷貝賦值操作符),則在編譯時,會出現鏈接錯誤 (link-time error)
為了將報錯提前到編譯時 (compile time),可增加了一個基類 Uncopyable,並將拷貝構造函數和拷貝賦值算子聲明為私有型,具體可參見 <Effective C++_3rd> item 6
在谷歌 C++ 編碼規范中,使用了一個宏定義來簡化,如下所示:
// 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 關鍵字
C++11 中,可在想要 “禁止使用” 的特殊成員函數聲明后加 “= delete”,而需要保留的加 "= default" 或者不采取操作
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 關鍵字可用於任何函數,不僅僅局限於類成員函數
3.1 函數重載
在函數重載中,可用 delete 來濾掉一些函數的形參類型,如下:
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
這樣在調用 IsLucky 函數時,如果參數類型不對,則會出現錯誤提示
if (IsLucky('a')) … // error ! call to deleted function if (IsLucky(true)) … // error ! if (IsLucky(3.5)) … // error !
3.2 模板特化
在模板特例化中,也可以用 delete 來過濾一些特定的形參類型。
例如,Widget 類中聲明了一個模板函數,當進行模板特化時,要求禁止參數為 void* 的函數調用。
如果按照 C++98 的 “私有不實現“ 思路,應該是將特例化的函數聲明為私有型,如下所示:
class Widget { public: template<typename T> void ProcessPointer(T* ptr) { … } private: template<> void ProcessPointer<void>(void*); // error! };
問題是,模板特化應該被寫在命名空間域 (namespace scope),而不是類域 (class scope),因此,該方法會報錯。
而在 C++11 中,因為有了 delete 關鍵字,則可以直接在類域外,將特例化的模板函數聲明為 delete, 如下所示:
class Widget { public: template<typename T> void ProcessPointer(T* ptr) { … } }; template<> void Widget::ProcessPointer<void>(void*) = delete; // still public, but deleted
這樣,當程序代碼中,有調用 void* 作形參的 ProcessPointer 函數時,則編譯時就會報錯。
小結:
1) Prefer deleted functions to private undefined ones
2) Any function may be deleted, including non-member functions and template instantiations
參考資料:
<C++ Primer_5th> chapter 13 Copy Control
<Effective C++_3rd> item 5 , item 6
<Effective Modern C++> item 11 , item 17