当前位置:网站首页>右值引用和移动构造
右值引用和移动构造
2022-07-29 05:25:00 【l_You_K】
右值引用
概念:左值:有地址的量就是左值。
右值:没有地址量就是右值。
常量因为在寄存器中存储 所以没有地址只有空间。要想引用常量就得 加const
例如 const int & i =10;
同样 我们也可以把它用来处理类对象,当类对象被临时对象初始化时,因为临时对象在代码运行后就会销毁,所以引用失败,虽然加了const 可以解决 const A& a = B(),但是这个对象成员属性我们不能拿来赋值操作。而且常对象只能访问常函数,(实际开发中我们不能要求别人的类成员函数成为常函数)。所以我们把对临时对象的常引用改成右值引用 A&& a = B();,可以延长临时对象存活周期,可以随意访问基类成员函数,可以改写此对象成员属性。具体看代码:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A的构造" << endl;
}
A(const A&)
{
cout << "A的拷贝构造" << endl;
}
virtual ~A()
{
cout << "A的析构" << endl;
}
virtual void showInfo()
{
cout << "您 好 啊" << endl;
}
};
class B : public A
{
public:
B()
{
cout << "B的构造" << endl;
}
~B()
{
cout << "B的析构" << endl;
}
void showInfo()override
{
cout << "同学!加油!!!!" << endl;
}
};
int main()
{
// const int& a = 10; //int temp = 10, const int& = temp;
//左值引用也叫常引用。
// const A& a = B();
A&& a = B();//右值引用应用场景之一 临时对象触发多态
a.showInfo();
//用了右引用之后就不会只能访问常函数了
//而且 这个对象也可以被赋值了 体现在移动构造
A&& a1 = std::move(a);//用已有的对象来初始化对象 move 其实返回的是临时对象 触发多态
a1.showInfo();
return 0;
}
移动语义函数std::move(左值)使用:主要是用来移动左值的空间资源的。
#include <iostream>
using namespace std;
class A
{
private:
int* p;
public:
A():p(new int[1024])
{
cout << "A的构造" << endl;
}
A(const A& other)
{
this->p = new int[1024];
memcpy(this->p,other.p,sizeof(int[1024]));
cout << "A的拷贝构造" << endl;
}
A(A&& other)//之所以传入的是右值引用就是因为 内部实现要对传进来的参数 进行操作
{
this->p = other.p;
other.p = nullptr;
cout << "A的移动构造" << endl;
}
~A()
{
if(p != nullptr)
{
delete [] p;
p = nullptr;
}
cout << "A的析构" << endl;
}
};
int main()
{
A a;
A a1 = std::move(a);//提高拷贝构造的效率 其实是隐构造 如果不写移动构造 调用的是拷贝构造
//A&& a1 = std::move(a); 其实也是起别名
}
边栏推荐
- Sliding window leetcode 76. minimum covering substring (hard) 76.76. minimumwindow substring (hard)
- UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
- UE4 天光和反射球的原理和区别
- LeetCode #7.整数反转
- LeetCode #189.轮转数组
- Ue5 light shadow basic shadow full resolution sawtooth shadow solution lumen
- LeetCode #167.两数之和 II - 输入有序数组
- LeetCode #876.链表的中间结点
- Number theory: proof that the maximum number that px+py cannot represent is pq-p-q
- Linked list -------------------------- tail insertion method
猜你喜欢
随机推荐
Ue5 landscape conversion Nanite conversion method and it does not support the use method of starting dynamic mesh with lumen and lumen
寒假集训总结 (1.23~1.28) [第一梯队]
Navicat for Oracle Cannot create oci environment
运算符重载
动态规划总结
Ue5 light shadow basic shadow full resolution sawtooth shadow solution lumen
官方教程 Redshift 05 system参数详细解释
LeetCode #283.移动零
9196 tumor area solution
Simple code to realize PDF to word document
FTP的两种模式详解
LeetCode #876.链表的中间结点
JUC concurrent knowledge points
Traditional model predictive control trajectory tracking - wavy trajectory (function package has been updated)
Leetcode 13. Roman numeral to integer
Leetcode 167. sum of two numbers II - input ordered array
Unity初学4——帧动画以及主角攻击(2d)
子网数、主机数与子网掩码的关系
Leetcode notes 605. can place flowers (easy) 605. planting flowers
[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?









