右值引用(及其支持的Move语意和完美转发)是C++0x将要加入的最重大语言特性之一。从实践角度讲,它能够完美解决C++中长久以来为人所诟病的临时对象效率问题。从语言本身讲,它健全了C++中的引用类型在左值右值方面的缺陷。从库设计者的角度讲,它给库设计者又带来了一把利器。从库使用者的角度讲,不动一兵一卒便可以获得“免费的”效率提升…
下面用实例来深入探讨右值引用。
1.什么是左值,什么是右值,简单说左值可以赋值,右值不可以赋值。以下面代码为例,“A a = getA();”该语句中a是左值,getA()的返回值是右值。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#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的移动构造函数,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#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类添加一个成员变量如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#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语句可以将左值变为右值而避免拷贝构造,修改代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#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.赋值操作符也可以是移动赋值。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#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语句实例解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 49
-
2025-05-29 47
-
2025-05-27 58
-
2025-05-25 52
-
2025-06-04 55

