C++類中的特殊成員函數(shù)示例詳解
前言
C++類中有幾個特殊的非靜態(tài)成員函數(shù),當(dāng)用戶未定義這些函數(shù)時,編譯器將給出默認(rèn)實現(xiàn)。C++11前有四個特殊函數(shù),C++11引入移動語義特性,增加了兩個參數(shù)為右值的特殊函數(shù)。這六個函數(shù)分別是:
1、默認(rèn)構(gòu)造函數(shù)
默認(rèn)構(gòu)造函數(shù)指不需要參數(shù)就能初始化的構(gòu)造函數(shù)。包含無參和所有參數(shù)有默認(rèn)值兩種類型的構(gòu)造函數(shù)。
2、復(fù)制構(gòu)造函數(shù)
復(fù)制構(gòu)造函數(shù)指使用該類的對象作為參數(shù)的構(gòu)造函數(shù)??梢杂衅渌麉?shù),但必須提供默認(rèn)值。
3、復(fù)制賦值運算符
重載等號=,將該類的對象賦值給已定義對象。
4、析構(gòu)函數(shù)
沒啥可說的。
5、移動構(gòu)造函數(shù)
C++11新增,該類的右值對象為參數(shù)的構(gòu)造函數(shù),其余同復(fù)制構(gòu)造函數(shù)。
6、移動復(fù)制運算符
同復(fù)制賦值運算符,唯一不同是參數(shù)為右值。
看定義容易迷糊,上代碼就會很清晰:
#include <iostream> #include <string> class Foo { public: std::string s; // 默認(rèn)構(gòu)造函數(shù) Foo() { std::cout << "default constructor" << std::endl; } // 復(fù)制構(gòu)造函數(shù) Foo(const Foo& foo) { std::cout << "copy constructor" << std::endl; s = foo.s; } // 復(fù)制賦值運算符 Foo& operator=(const Foo& foo) { std::cout << "copy assignment operator" << std::endl; s = foo.s; return * this;} // 移動構(gòu)造函數(shù) Foo(Foo&& foo) { std::cout << "move constructor" << std::endl; s = std::move(foo.s); } // 移動賦值運算符 Foo& operator=(Foo&& foo) { std::cout << "move assignment operator" << std::endl; s = std::move(foo.s); return *this;} }; int main() { Foo foo1; Foo foo2(foo1); foo1 = foo2; Foo foo3(std::move(foo1)); foo2 = std::move(foo3); }
用g++或者clang編譯,加上-fno-elide-constructors -std=c++0x選項。執(zhí)行程序輸出如下:
default constructor
copy constructor
copy assignment operator
move constructor
move assignment operator
結(jié)果是我們預(yù)期的。需要注意的是Foo foo3 = foo1的形式會調(diào)用復(fù)制構(gòu)造函數(shù),不會調(diào)用復(fù)制賦值運算符。原因是Foo foo3 = xxx聲明和定義一個新對象,而賦值是作用在已定義對象。移動賦值運算符同理。
C++11新增了=default和=delete函數(shù)修飾符,提示編譯器使用默認(rèn)或者刪除默認(rèn)的特殊函數(shù)。需要注意的是這兩個修飾符只能修飾上述特殊函數(shù),用戶可以用其對特殊函數(shù)進行裁剪。一個例子:
struct Test { // 使用默認(rèn)構(gòu)造函數(shù) Test() = default; // 刪除復(fù)制賦值運算符 Test& operator=(const Test& test) = delete; // 使用默認(rèn)析構(gòu)函數(shù) ~Test() = default; };
參考
- https://en.cppreference.com/w/cpp/language/member_functions
- https://stackoverflow.com/questions/43349808/extended-lifetime-of-an-object-returned-from-function
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)
- 01-10深入理解鏈表的各類操作詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery