c++11 中做值引用与右值引用
重点理解:
- 左值引用与右值引用的区别?
- 左值引用与右值引有什么特例?
- 什么是移动语义?解决哪类问题?
- 什么是完美转发?
- 什么是将亡值?
实现移动语义
- 通过实现移动语义,对象赋值时,避免资源的重新分配。c++11 之前利用深拷贝解决浅拷贝问题;
- STL中应用非常广泛;
- 实现 std::unique_ptr 等等。
解决深拷贝问题
什么是深拷贝?什么情况下使用深拷贝? 请参考深拷贝与浅拷贝。
#include <iostream>
#include <list>
#include <cstring>
class A {
public:
A() {
p = new int(10);
cout << "A():p=" << p << endl;
}
A(const A&a) { // 深拷贝和浅拷贝
// p= a.p;
// a.p = nullptr;
p = new int(10);
memcpy(p, a.p, 10*sizeof(int));
cout << "A(const A&):p=" << p << endl;
}
~A() {
if (p != nullptr) {
delete []p;
p = nullptr;
cout << "~A()1" << endl;
} else {
cout << "~A()2" << endl;
}
}
A(A&& a) {
this->p = a.p;
a.p = nullptr;
cout << "A(A&&)" << endl;
}
int *p;
};
int main () {
A a;
A a1(a); //调用拷贝构造函数
cout << "a.p的地址为: " << a.p << " a1.p的地址为: " << a1.p << endl;
cout << "======触发移动构造======" << endl;
A a2(std::move(a)); //触发移动构造
cout << "a2.p的地址为:" << a2.p << " a.p为" << a.p << endl;
}
代码执行结果:
A():p=0x862c20
A(const A&):p=0x862c40
a.p的地址为: 0x862c20 a1.p的地址为: 0x862c40
======触发移动构造======
A(A&&)
a2.p的地址为:0x862c20 a.p为0 # 对象a的资源转移到a2之后调用析构将自己销毁。
~A()1
~A()1
~A()2
STL中的应用
- 将堆上的资源移动到容器当中
#include <iostream>
#include <list>
#include <cstring>
using std::cout;
using std::endl;
class A {
public:
A() {
p = new int(10);
cout << "A():p=" << p << endl;
}
A(const A&a) { // 深拷贝和浅拷贝
// p= a.p;
// a.p = nullptr;
p = new int(10);
memcpy(p, a.p, 10*sizeof(int));
cout << "A(const A&):p=" << p << endl;
}
~A() {
if (p != nullptr) {
delete []p;
p = nullptr;
cout << "~A()1" << endl;
} else {
cout << "~A()2" << endl;
}
}
A(A&& a) {
this->p = a.p;
a.p = nullptr;
cout << "A(A&&)" << endl;
}
int *p;
};
int main () {
list<A> alist;
A a;
alist.push_back(A()); //(1) A()匿名对象,右值,直接调用移动构造变成一个将亡值
//alist.push_back(a); //(2) 左值,具体的对象,调用拷贝构造
//alist.push_back(std::move(a)); //(3) 将左值变为右值,调用移动构造
auto &front = alist.front();
cout << "front.p=" << front.p << endl;
return 0;
}
代码执行结果:
(1) 匿名对象A()申请堆上的地址与容器中A()地址相同,说明移动资源成功
A():p=0x1f60c20 #调用了构造函数
A(A&&) #调用了移动构造
~A()2 #析构将亡值
front.p=0x1f60c20
~A()1 #析构alist对象
(2)插入左值,调用拷贝构造函数,重新申请堆上的空间。对象地址不相同
A():p=0x7f2c20
A(const A&):p=0x7f2c60
front.p=0x7f2c60
~A()1
~A()1
(3)将左值转化为右值,调用了移动构造
A():p=0x178fc20
A(A&&)
front.p=0x178fc20
~A()2
~A()1
在unique_ptr的应
- 明确对象只有一个拥有者;
unique_ptr
没有拷贝构造、没有赋值运算符、仅提供移动构造和移动赋值。
智能指针中
unique_ptr
相关内容可以参考unique_ptr相关介绍。
实现完美转发
- 理解什么是完美?什么是不完美?
- 怎么解决完美转发?
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::forward;
void func(int &n) {
cout << "lvalue=" << n << endl;
}
void func(int &&n) {
cout << "rvalue=" << n << endl;
}
template<typename T>
void revoke(T&&t) {
func(t); //(1)
//func(std::forward<T>(t)); //(2)
}
int main() {
//(1)
int i = 10;
int &m = i; //左值引用
int &&n = 100; //右值引用
revoke(m);
revoke(n);
//(2)
//revoke(i);
//revoke(10);
return 0;
}
代码执行结果:
(1)
lvalue=10
lvalue=100
(2)
lvalue=10
rvalue=10
思考:
为什么没有将右值引用int &&n = 100;
转发成功?
答:因为声明出来的左值引用或右值引用都是左值!
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::forward;
void func(int &n) {
cout << "lvalue=" << n << endl;
}
void func(int &&n) {
cout << "rvalue=" << n << endl;
}
template<typename T>
void revoke(T&&t) {
func(std::forward<T>(t));
}
int main() {
int i = 10;
int &m = i; //左值引用
int &&n = 100; //右值引用
revoke(m);
revoke(n);
}
代码执行结果:
lvalue=10
lvalue=100
充电站
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习