C++11右值引用和std::move語句實例解析(推薦)
右值引用(及其支持的Move語意和完美轉(zhuǎn)發(fā))是C++0x將要加入的最重大語言特性之一。從實踐角度講,它能夠完美解決C++中長久以來為人所詬病的臨時對象效率問題。從語言本身講,它健全了C++中的引用類型在左值右值方面的缺陷。從庫設計者的角度講,它給庫設計者又帶來了一把利器。從庫使用者的角度講,不動一兵一卒便可以獲得“免費的”效率提升…
下面用實例來深入探討右值引用。
1.什么是左值,什么是右值,簡單說左值可以賦值,右值不可以賦值。以下面代碼為例,“A a = getA();”該語句中a是左值,getA()的返回值是右值。
#include "stdafx.h" #include <iostream> class A { public: A() { std::cout << "Constructor" << std::endl; } A(const A&) { std::cout << "Copy Constructor" << std::endl; } ~A() {} }; static A getA() { A a; return a; } int main() { A a = getA(); return 0; }
運行以上代碼,輸出結(jié)果如下:
Constructor
Copy Constructor
可以看到A的構(gòu)造函數(shù)調(diào)用一次,拷貝構(gòu)造函數(shù)調(diào)用了一次,構(gòu)造函數(shù)和拷貝構(gòu)造函數(shù)是消耗比較大的,這里是否可以避免拷貝構(gòu)造?C++11做到了這一點。
2.添加A的移動構(gòu)造函數(shù),代碼如下:
#include "stdafx.h" #include <iostream> class A { public: A() { std::cout << "Constructor" << std::endl; } A(const A&) { std::cout << "Copy Constructor" << std::endl; } A(const A&&) { std::cout << "Move Constructor" << std::endl; } ~A() {} }; static A getA() { A a; return a; } int main() { A a = getA(); return 0; }
運行以上代碼,輸出結(jié)果:
Constructor
Move Constructor
這樣就沒有調(diào)用拷貝構(gòu)造函數(shù),而是調(diào)用移動構(gòu)造。這里并沒有看到移動構(gòu)造的優(yōu)點。
3.修改代碼,給A類添加一個成員變量如下:
#include "stdafx.h" #include <iostream> #include <vector> class B { public: B() {} B(const B&) { std::cout << "B Constructor" << std::endl; } }; class A { public: A(): m_b(new B()) { std::cout << "A Constructor" << std::endl; } A(const A& src) : m_b(new B(*(src.m_b))) { std::cout << "A Copy Constructor" << std::endl; } A(A&& src) : m_b(src.m_b) { src.m_b = nullptr; std::cout << "A Move Constructor" << std::endl; } ~A() { delete m_b; } private: B* m_b; }; static A getA() { A a; std::cout << "================================================" << std::endl; return a; } int main() { A a = getA(); std::cout << "================================================" << std::endl; A a1(a); return 0; }
運行以上代碼,輸出結(jié)果:
A Constructor
================================================
A Move Constructor
================================================
B Constructor
A Copy Constructor
“A a = getA();”調(diào)用的是A的移動構(gòu)造,“A a1(a);”調(diào)用的是A的拷貝構(gòu)造。A的拷貝構(gòu)造需要對成員變量B進行深拷貝,而A的移動構(gòu)造不需要,很明顯,A的移動構(gòu)造效率高。
4.std::move語句可以將左值變?yōu)橛抑刀苊饪截悩?gòu)造,修改代碼如下:
#include "stdafx.h" #include <iostream> #include <vector> class B { public: B() {} B(const B&) { std::cout << "B Constructor" << std::endl; } }; class A { public: A(): m_b(new B()) { std::cout << "A Constructor" << std::endl; } A(const A& src) : m_b(new B(*(src.m_b))) { std::cout << "A Copy Constructor" << std::endl; } A(A&& src) : m_b(src.m_b) { src.m_b = nullptr; std::cout << "A Move Constructor" << std::endl; } ~A() { delete m_b; } private: B* m_b; }; static A getA() { A a; std::cout << "================================================" << std::endl; return a; } int main() { A a = getA(); std::cout << "================================================" << std::endl; A a1(a); std::cout << "================================================" << std::endl; A a2(std::move(a1)); return 0; }
運行以上代碼,輸出結(jié)果:
A Constructor
================================================
A Move Constructor
================================================
B Constructor
A Copy Constructor
================================================
A Move Constructor
“A a2(std::move(a1));”將a1轉(zhuǎn)換為右值,因此a2調(diào)用的移動構(gòu)造而不是拷貝構(gòu)造。
5.賦值操作符也可以是移動賦值。
#include "stdafx.h" #include <iostream> #include <vector> class B { public: B() {} B(const B&) { std::cout << "B Constructor" << std::endl; } }; class A { public: A(): m_b(new B()) { std::cout << "A Constructor" << std::endl; } A(const A& src) : m_b(new B(*(src.m_b))) { std::cout << "A Copy Constructor" << std::endl; } A(A&& src) : m_b(src.m_b) { src.m_b = nullptr; std::cout << "A Move Constructor" << std::endl; } A& operator=(const A& src) { if (this == &src) return *this; m_b = new B(*(src.m_b)); std::cout << "operator=(const A& src)" << std::endl; return *this; } A& operator=(A&& src) { if (this == &src) return *this; m_b = src.m_b; src.m_b = nullptr; std::cout << "operator=(const A&& src)" << std::endl; return *this; } ~A() { delete m_b; } private: B* m_b; }; static A getA() { A a; std::cout << "================================================" << std::endl; return a; } int main() { A a = getA();//移動構(gòu)造 std::cout << "================================================" << std::endl; A a1(a);//拷貝構(gòu)造 std::cout << "================================================" << std::endl; A a2(std::move(a1));//移動構(gòu)造 std::cout << "================================================" << std::endl; a2 = getA();//移動賦值 std::cout << "================================================" << std::endl; a2 = a1;//拷貝賦值 return 0; }
運行以上代碼,輸出結(jié)果:
A Constructor
================================================
A Move Constructor
================================================
B Constructor
A Copy Constructor
================================================
A Move Constructor
================================================
A Constructor
================================================
A Move Constructor
operator=(const A&& src)
================================================
B Constructor
operator=(const A& src)
總之盡量給類添加移動構(gòu)造和移動賦值函數(shù),而減少拷貝構(gòu)造和拷貝賦值的消耗。
以上所述是小編給大家介紹的C++11右值引用和std::move語句實例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
您可能感興趣的文章
- 01-10探討:C++中函數(shù)返回引用的注意事項
- 01-10c++中拷貝構(gòu)造函數(shù)的參數(shù)類型必須是引用
- 01-10引用參數(shù)和傳值參數(shù)的區(qū)別深入解析
- 01-10淺析C++中結(jié)構(gòu)體的定義、初始化和引用
- 01-10深入解析C++中的引用類型
- 01-10關于&quot;引用&quot;的幾點說明介紹
- 01-10C++中引用(&amp;)的用法與應用實例分析
- 01-10淺析C和C++函數(shù)的相互引用
- 01-10C/C++中指針和引用之相關問題深入研究
- 01-10C++中對象的常引用總結(jié)


閱讀排行
本欄相關
- 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-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery