当前位置:网站首页>Right value reference and mobile construction
Right value reference and mobile construction
2022-07-29 06:39:00 【l_ You_ K】
Right quoting
Concept : The left value : The quantity with address is the lvalue .
Right value : No address quantity is the right value .
Constants are stored in registers So there is no address, only space . To reference constants, you have to Add const
for example const int & i =10;
Again We can also use it to deal with class objects , When a class object is initialized by a temporary object , Because temporary objects will be destroyed after the code runs , So the reference failed , Although added const Can solve const A& a = B(), But we can't assign the member attribute of this object . And constant objects can only access constant functions ,( In actual development, we can't require others' class member functions to become constant functions ). So we change the regular reference to the temporary object to the right value reference A&& a = B();, You can extend the lifetime of temporary objects , You can access base class member functions at will , You can override the member properties of this object . See code for details :
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A Construction " << endl;
}
A(const A&)
{
cout << "A Copy construction of " << endl;
}
virtual ~A()
{
cout << "A The analysis of " << endl;
}
virtual void showInfo()
{
cout << " you good ah " << endl;
}
};
class B : public A
{
public:
B()
{
cout << "B Construction " << endl;
}
~B()
{
cout << "B The analysis of " << endl;
}
void showInfo()override
{
cout << " classmate ! come on. !!!!" << endl;
}
};
int main()
{
// const int& a = 10; //int temp = 10, const int& = temp;
// Lvalue reference is also called constant reference .
// const A& a = B();
A&& a = B();// The right value refers to one of the application scenarios Temporary objects trigger polymorphism
a.showInfo();
// After using the right reference, you can't only access constant functions
// and This object can also be assigned Embodied in the mobile structure
A&& a1 = std::move(a);// Initialize objects with existing objects move In fact, what is returned is a temporary object Trigger polymorphism
a1.showInfo();
return 0;
}
Mobile semantic function std::move( The left value ) Use : It is mainly used to move the spatial resources of lvalues .
#include <iostream>
using namespace std;
class A
{
private:
int* p;
public:
A():p(new int[1024])
{
cout << "A Construction " << endl;
}
A(const A& other)
{
this->p = new int[1024];
memcpy(this->p,other.p,sizeof(int[1024]));
cout << "A Copy construction of " << endl;
}
A(A&& other)// The reason why the right value reference is passed in is because The internal implementation needs to control the parameters passed in To operate
{
this->p = other.p;
other.p = nullptr;
cout << "A Mobile structure of " << endl;
}
~A()
{
if(p != nullptr)
{
delete [] p;
p = nullptr;
}
cout << "A The analysis of " << endl;
}
};
int main()
{
A a;
A a1 = std::move(a);// Improve the efficiency of copy construction It's actually an implicit structure If you don't write the mobile structure What is called is a copy construct
//A&& a1 = std::move(a); In fact, it is also an alias
}
边栏推荐
- 虹科分享 | 带您全面认识“CAN总线错误”(一)——CAN总线错误与错误帧
- 虹科白皮书 | 在工业4.0阶段,如何利用TSN时间敏感网络技术打造数字化工厂?
- 2022 summer second day information competition learning achievement sharing 1
- Merkle tree existential function modified for the first time
- [interview questions] the latest software test interview questions in 2022 (400) [with answers] continue to update
- 右值引用和移动构造
- 虹科Automation softPLC | MoDK运行环境与搭建步骤(1)——运行环境简介
- Day16 set
- On defect description style
- Merkle Tree 存在性功能第一次修改
猜你喜欢
虹科为您分享EtherCAT demo,教您如何从其他协议快速过渡到EtherCAT工业总线
Circular linked list and bidirectional linked list
Vivado IP核之定点数转为浮点数Floating-point
day09_static&final&代码块&抽象类&接口&内部类
3、 Wide area communication network
Vivado IP核之复数浮点数乘法 Floating-point
[leetcode brush questions] array 3 - divide and conquer
虹科案例 | PAC:一种整合了softPLC控制逻辑、HMI和其他服务功能的集成控制解决方案
Network Security Learning (II)
虹科方案 | 在数字化的变电站中低成本实现无缝集成的独特解决方案
随机推荐
Advanced socket programming (options and control information)
day03_2_作业
Webshell管理工具的流量特征
虹科 | 使用JESD204串行接口高速桥接模拟和数字世界
虹科Automation softPLC | 虹科KPA MoDK运行环境与搭建步骤(2)——MoDK运行环境搭建
What is the lifecycle of automated testing?
用神经网络实现手写数字识别
UDP套接口通信实验
虹科白皮书 | 在工业4.0阶段,如何利用TSN时间敏感网络技术打造数字化工厂?
Explain the difference between FIR filter and IIR filter in detail
六、 网络互联与互联网
右值引用和移动构造
Those vulnerability attacks on app
day02_基本语法
解决分频模块modelsim下仿真输出为stx的错误
Vivado IP核之复数浮点数除法 Floating-point
day10_ Exception handling & enumeration
Summary of winter vacation training (1.23~1.28) [first tier]
关于DDoS的几个误区
不安全的第三方组件的漏洞如何做前置规避?