雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

右值引用(及其支持的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

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有