欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++11中delete和default的用法詳解

 更新時(shí)間:2023年08月31日 08:31:35   作者:飛鳶逐浪  
這篇文章主要為大家詳細(xì)介紹了C++11中delete和default的具體用法,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下

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ù)

作為開發(fā)者,如果不想讓用戶使用某個(gè)成員函數(shù),不聲明即可;但對于特殊成員函數(shù),則是另一種情況

例如,設(shè)計(jì)一個(gè)樹葉類

class LeafOfTree{ 
  // ... 
};     

萊布尼茨說過,"世上沒有兩片完全相同的樹葉" (Es gibt keine zwei Blätter, die gleich bleiben),因此,對于一片獨(dú)一無二的樹葉,下面的操作是錯(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ù)和拷貝賦值算子聲明為私有型,具體可參見 《Effective C++》3rd item 6

在較早的谷歌 C++ 編碼規(guī)范中,使用了一個(gè)宏來簡化,如下:

// 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 來濾掉一些函數(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ù)類型不對,則會(huì)出現(xiàn)錯(cuò)誤提示

if (IsLucky('a')) …     // error !    call to deleted function
if (IsLucky(true)) …    // error !
if (IsLucky(3.5)) …     // error !   

3.2 模板特例化

在模板特例化中,也可以用 delete 來過濾一些特定的形參類型。

例如,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!
};  

問題是,模板特例化應(yīng)當(dāng)被寫在命名空間域 (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ì)起作用??傊?,沒有 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)文章

最新評論