当前位置:网站首页>右值引用和移动构造
右值引用和移动构造
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); 其实也是起别名
}
边栏推荐
- Add time series index to two-dimensional table
- [beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
- Leetcode 19. delete the penultimate node of the linked list
- Learning notes of bit operation
- UE4/UE5 C盘变大处理
- 虹科分享 | 带您全面认识“CAN总线错误”(一)——CAN总线错误与错误帧
- THINKPHP5 常见问题
- 官方教程 Redshift 02 4中GI引擎概述及总结
- Redshift 2.6.41 for maya2018 水印去除
- [leetcode brush questions] array 3 - divide and conquer
猜你喜欢

虹科白皮书 | 在工业4.0阶段,如何利用TSN时间敏感网络技术打造数字化工厂?

leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)

Computer factory interview questions

虹科分享 | 带您全面认识“CAN总线错误”(一)——CAN总线错误与错误帧
![[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?](/img/e2/56234084d0cfad6906f9e84212182a.png)
[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?

Leetcode 977. Square of ordered array

LeetCode #167.两数之和 II - 输入有序数组

Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)

虹科 | 使用JESD204串行接口高速桥接模拟和数字世界

SQL Developer图形化窗口创建数据库(表空间和用户)
随机推荐
LeetCode #13. 罗马数字转整数
Navicat for Oracle Cannot create oci environment
摊余成本最牛例子
虹科 | 使用JESD204串行接口高速桥接模拟和数字世界
虹科分享 | 带您全面认识“CAN总线错误”(一)——CAN总线错误与错误帧
LeetCode #19.删除链表的倒数第N个结点
Rowkey设计
Abstract encapsulation inheritance polymorphism
虹科分享 | 带你全面认识“CAN总线错误”(二)——CAN错误类型
Leetcode 14. longest public prefix
LeetCode #1.两数之和
Learning notes of bit operation
虹科分享 | 测试与验证复杂的FPGA设计(2)——如何在IP核中执行面向全局的仿真
Leetcode 1. sum of two numbers
Leetcode 344. reverse string
LeetCode #283.移动零
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
JUC concurrent knowledge points
Leetcode 83. delete duplicate elements in the sorting linked list
#6898 变幻的矩阵 题解