200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > [C++11]右值和右值引用

[C++11]右值和右值引用

时间:2019-11-27 13:12:28

相关推荐

[C++11]右值和右值引用

代码如下:

#include <iostream>using namespace std;int main(){//左值int num = 9;//左值引用int &a = num;//右值const int N = 5;//右值引用int && b = 8;//常量左值引用const int &c = num;//常量右值引用const int &&d = 6;//const int &&d = b; error//int && f = b; error//右值引用给常量的左值引用初始化const int &a = b;//常量右值引用给常量的右值引用初始化const int &aa = d;}

右值引用的作用以及使用:

代码如下:

#include <iostream>using namespace std;class Test{public:Test() :m_num(new int(100)){cout << "construct" << endl;cout << "address = " << m_num << endl;}Test(const Test & a) :m_num(new int(*a.m_num)){cout << "copy construct" << endl;}~Test(){cout << "destruct" << endl;delete m_num;}public:int *m_num;};Test getobj(){Test t;return t;}int main(){Test t = getobj();return 0;}

测试结果:

代码如下:

#include <iostream>using namespace std;class Test{public:Test() :m_num(new int(100)){cout << "construct" << endl;cout << "address = " << m_num << endl;}Test(const Test & a) :m_num(new int(*a.m_num)){cout << "copy construct" << endl;}//有右值引用的构造函数称为移动构造函数//移动构造函数 -> 复用其他对象中的资源(堆内存)//m_num 浅拷贝Test(Test && a) :m_num(a.m_num){a.m_num = nullptr;cout << "move construct" << endl;}~Test(){cout << "destruct" << endl;delete m_num;}public:int *m_num;};Test getobj(){Test t;return t;}int main(){Test t = getobj();//直接赋值return 0;}

测试结果:

因为getobj()返回的是临时变量,所以程序优先调用移动构造函数,如果返回的不是临时对象,就会调用拷贝构造函数。

通过右值引用赋值:

代码如下:

int main(){Test && t1 = getobj();cout <<"address = "<< t1.m_num << endl;return 0;}

测试结果:

不管是直接进行赋值,还是右值引用赋值,都要求右侧的对象是一个临时对象,才会调用移动构造函数。

如果没有移动构造函数,它会调用拷贝构造函数。

如果没有移动构造函数,使用右值引用初始化要求更高一点。

要求右侧是一个临时的不能取地址的对象。

代码如下:

Test getobj(){return Test();}int main(){Test && t2 = getobj();cout <<"address = "<< t2.m_num << endl;return 0;}

测试结果:

代码如下:

Test&& getobj()//将亡值{return Test();}int main(){Test && t2 = getobj();cout <<"address = "<< t2.m_num << endl;return 0;}

测试结果:

&&的特性:

右值引用被推导或被传递之后对应的就是一个左值或者右值。

以上图片来自下面链接:

/cpp/rvalue-reference/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。