国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++11右值引用和std::move語句實例解析(推薦)

2020-05-23 13:49:06
字體:
來源:轉載
供稿:網友

右值引用(及其支持的Move語意和完美轉發)是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;}

運行以上代碼,輸出結果如下:

Constructor
Copy Constructor

可以看到A的構造函數調用一次,拷貝構造函數調用了一次,構造函數和拷貝構造函數是消耗比較大的,這里是否可以避免拷貝構造?C++11做到了這一點。

2.添加A的移動構造函數,代碼如下:

#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;}

運行以上代碼,輸出結果:

Constructor
Move Constructor

這樣就沒有調用拷貝構造函數,而是調用移動構造。這里并沒有看到移動構造的優點。

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;}

運行以上代碼,輸出結果:

A Constructor
================================================
A Move Constructor
================================================
B Constructor
A Copy Constructor

“A a = getA();”調用的是A的移動構造,“A a1(a);”調用的是A的拷貝構造。A的拷貝構造需要對成員變量B進行深拷貝,而A的移動構造不需要,很明顯,A的移動構造效率高。

4.std::move語句可以將左值變為右值而避免拷貝構造,修改代碼如下:

#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;}

運行以上代碼,輸出結果:

A Constructor
================================================
A Move Constructor
================================================
B Constructor
A Copy Constructor
================================================
A Move Constructor

“A a2(std::move(a1));”將a1轉換為右值,因此a2調用的移動構造而不是拷貝構造。

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();//移動構造  std::cout << "================================================" << std::endl;  A a1(a);//拷貝構造  std::cout << "================================================" << std::endl;  A a2(std::move(a1));//移動構造  std::cout << "================================================" << std::endl;  a2 = getA();//移動賦值  std::cout << "================================================" << std::endl;  a2 = a1;//拷貝賦值  return 0;}

運行以上代碼,輸出結果:

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)

 總之盡量給類添加移動構造和移動賦值函數,而減少拷貝構造和拷貝賦值的消耗。

以上所述是小編給大家介紹的C++11右值引用和std::move語句實例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临安市| 美姑县| 临湘市| 丰顺县| 仲巴县| 株洲市| 弥勒县| 曲水县| 巴林右旗| 山东| 临西县| 翁牛特旗| 高尔夫| 五指山市| 永春县| 襄汾县| 四子王旗| 原阳县| 湘潭市| 辽宁省| 河东区| 余江县| 昭通市| 安溪县| 东丽区| 磐石市| 五原县| 于都县| 沅江市| 喀什市| 阿图什市| 尚志市| 逊克县| 四平市| 泸定县| 马边| 绥化市| 郎溪县| 平乐县| 琼海市| 广元市|