Effective C++ 條款5

來源:互聯網
上載者:User

標籤:default

瞭解C++預設編寫並調用哪些函數

本節有兩處知識點。

首先
對於一個class
1.如果class中沒有任何建構函式,那麼編譯器為class聲明一個default預設建構函式。
2.如果class沒有解構函式、copy建構函式、copy assignment運算子則class聲明這三個函數。
3.只有當調用解構函式、copy建構函式、copy assignment運算子時,編譯器才產生它們,請大家分清楚,聲明和產生是兩碼事。

如下代碼:

//定義的classclass Empty{};//編譯器聲明的classclass Empty{public:    Empty(){}    Empty(const Empty& rhs){}    Empty& operator=(const Empty& rhs){}    ~Empty(){}};
其次

當類中存在參考型別的資料或者const類型資料。則編譯器拒絕自動產生copy assignment運算子函數。除非你自己定義
如下代碼:

//對於這個類,編譯器不會自動產生copy assignment template<typename T>class NamedObject{public:    NamedObject(const char* name, const T& value);    NamedObject(const std::string& name, const T& value);private:    std::string& nameValue;    const T objectValue;};

Effective C++ 條款5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.