C++11右值引用和std::move語(yǔ)句實(shí)例解析(推薦)
右值引用(及其支持的Move語(yǔ)意和完美轉(zhuǎn)發(fā))是C++0x將要加入的最重大語(yǔ)言特性之一。從實(shí)踐角度講,它能夠完美解決C++中長(zhǎng)久以來(lái)為人所詬病的臨時(shí)對(duì)象效率問(wèn)題。從語(yǔ)言本身講,它健全了C++中的引用類型在左值右值方面的缺陷。從庫(kù)設(shè)計(jì)者的角度講,它給庫(kù)設(shè)計(jì)者又帶來(lái)了一把利器。從庫(kù)使用者的角度講,不動(dòng)一兵一卒便可以獲得“免費(fèi)的”效率提升…
下面用實(shí)例來(lái)深入探討右值引用。
1.什么是左值,什么是右值,簡(jiǎn)單說(shuō)左值可以賦值,右值不可以賦值。以下面代碼為例,“A a = getA();”該語(yǔ)句中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;
}
運(yùn)行以上代碼,輸出結(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做到了這一點(diǎn)。
2.添加A的移動(dòng)構(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;
}
運(yùn)行以上代碼,輸出結(jié)果:
Constructor
Move Constructor
這樣就沒(méi)有調(diào)用拷貝構(gòu)造函數(shù),而是調(diào)用移動(dòng)構(gòu)造。這里并沒(méi)有看到移動(dòng)構(gòu)造的優(yōu)點(diǎn)。
3.修改代碼,給A類添加一個(gè)成員變量如下:
#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;
}
運(yùn)行以上代碼,輸出結(jié)果:
A Constructor
================================================
A Move Constructor
================================================
B Constructor
A Copy Constructor
“A a = getA();”調(diào)用的是A的移動(dòng)構(gòu)造,“A a1(a);”調(diào)用的是A的拷貝構(gòu)造。A的拷貝構(gòu)造需要對(duì)成員變量B進(jìn)行深拷貝,而A的移動(dòng)構(gòu)造不需要,很明顯,A的移動(dòng)構(gòu)造效率高。
4.std::move語(yǔ)句可以將左值變?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;
}
運(yùn)行以上代碼,輸出結(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)用的移動(dòng)構(gòu)造而不是拷貝構(gòu)造。
5.賦值操作符也可以是移動(dòng)賦值。
#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();//移動(dòng)構(gòu)造
std::cout << "================================================" << std::endl;
A a1(a);//拷貝構(gòu)造
std::cout << "================================================" << std::endl;
A a2(std::move(a1));//移動(dòng)構(gòu)造
std::cout << "================================================" << std::endl;
a2 = getA();//移動(dòng)賦值
std::cout << "================================================" << std::endl;
a2 = a1;//拷貝賦值
return 0;
}
運(yùn)行以上代碼,輸出結(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)
總之盡量給類添加移動(dòng)構(gòu)造和移動(dòng)賦值函數(shù),而減少拷貝構(gòu)造和拷貝賦值的消耗。
以上所述是小編給大家介紹的C++11右值引用和std::move語(yǔ)句實(shí)例解析,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
上一篇:C語(yǔ)言開(kāi)發(fā)之歸并排序詳解及實(shí)例
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言 指針與數(shù)組的詳解及區(qū)別
本文標(biāo)題:C++11右值引用和std::move語(yǔ)句實(shí)例解析(推薦)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1688.html
您可能感興趣的文章
- 01-10探討:C++中函數(shù)返回引用的注意事項(xiàng)
- 01-10c++中拷貝構(gòu)造函數(shù)的參數(shù)類型必須是引用
- 01-10引用參數(shù)和傳值參數(shù)的區(qū)別深入解析
- 01-10淺析C++中結(jié)構(gòu)體的定義、初始化和引用
- 01-10深入解析C++中的引用類型
- 01-10關(guān)于&quot;引用&quot;的幾點(diǎn)說(shuō)明介紹
- 01-10C++中引用(&amp;)的用法與應(yīng)用實(shí)例分析
- 01-10淺析C和C++函數(shù)的相互引用
- 01-10C/C++中指針和引用之相關(guān)問(wèn)題深入研究
- 01-10C++中對(duì)象的常引用總結(jié)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery


