C++11中右值引用和std::move语句的示例分析

发布时间:2021-09-03 13:46:48 作者:小新
来源:亿速云 阅读:88

这篇文章主要介绍了C++11中右值引用和std::move语句的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

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语句的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. C++ 模板(一)
  2. C++ 继承(二)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:如何调试Django时打印SQL语句的日志

下一篇:MySQL中的隐藏列的具体查看方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》