当前位置:网站首页>When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
2022-07-07 10:21:00 【qq_ forty-two million one hundred and twenty thousand eight hun】
Problem description : For custom types that contain Pointer type member variable When , Assignment operator overload Time function Return value and Parameters Why does the type have to be Reference type ?
#include<iostream>
using namespace std;
// Assignment operator overload
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
Person & operator=(Person & p) //14 That's ok
{
// If there is unreleased heap space , Release first
if (m_Age != NULL)
{
delete m_Age;//pos1
m_Age = NULL;
}
m_Age = new int(*p.m_Age);// notes : Here, the newly allocated memory space address may be the same as the above pos1 The address of the memory space released at is the same ( The reason for the compiler )
return *this;
}
~Person()//28 That's ok
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
int* m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //46 That's ok
cout << "p1 The age is :" << *p1.m_Age << endl;
cout << "p2 The age is :" << *p2.m_Age << endl;
cout << "p3 The age is :" << *p3.m_Age << endl;
}
int main(void)
{
test01();
system("pause");
return 0;
}
case 1:
The return value must be of reference type The reason of : The assignment operator is overloaded in , If the return value is a value type, that is
14 That's okTurn into
Person operator=(Person & p)
That is, the return value is of value type , Then the return value type after the function call , For the call stack of external functions, it is a Temporary objects .
and The temporary object is a right value ( It doesn't exist in the stack memory , The life cycle ends after a complete expression , namely p3 = p2 = p1; End after statement execution ), Objects that do not exist in stack memory cannot be referenced with ordinary lvalues ( such as Person & ) To quote , therefore 46 That's ok Of p2 = p1 The result is a Temporary objects , That is, a Right value ,p3=(p2 = p1) Call to p3.operator(p2=p1 Returned object ),Person & Is an lvalue reference , and p2 = p1 The return result is the right value ( The life cycle is short ), After statement end ,p3 Facing the danger of ineffectiveness , So the compiler is not allowed to pass .
contrary , if The return value is the reference type That is to say, what is returned is Person Object itself Not temporary objects , be p2 = p1 Back to a The left value ,p3 = (p2 = p1), Call to p3.operator(p2=p1 Returned object ) Is to give a Space that can exist for a long time in memory Alias to manipulate , So it can be compiled .
- case2: Parameter type must be reference type The reason of : The parameter type is the value type, that is
14 That's okTurn into
Person & operator=(Person p)
p3 = p2 = p1 Execute first p2 = p1, That is equivalent to p2.operator=(p1), The object passed in is p1 A copy of , be aware One of the objects is Pointer variable member m_Age, When copying Directly copy its value , meanwhile , The parameter passed in is Local objects , It will be released after the function is executed , Then it will call 28 Line destructor , Now p1 Of m_Age Memory space will be released , Followed by execution p3 = (p2 = p1), That is to call p3.operator=(p2=p1 Returned object ), Will be p2 Of m_Age Memory space is released , But when 46 Yes p3 = p2 = p1 After execution , Then the corresponding destructor will be executed again , take p1 Of m_Age To release , The memory space has been released at the end of the function call , As a result Secondary deconstruction ( Release memory space repeatedly ).
But the specific implementation process , It is suggested that you should clearly understand the cause of this exception through your own debugging .
边栏推荐
- The Himalaya web version will pop up after each pause. It is recommended to download the client solution
- conda离线创建虚拟环境
- [second on] [jeecgboot] modify paging parameters
- Es classes and objects, prototypes
- STM32 product introduction
- JMeter installation
- Enterprise practice | construction of banking operation and maintenance index system under complex business relations
- Appx代码签名指南
- 【acwing】789. Range of numbers (binary basis)
- Prototype object in ES6
猜你喜欢

AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these

request对象对请求体,请求头参数的解析

The landing practice of ByteDance kitex in SEMA e-commerce scene

ORM--数据库增删改查操作逻辑

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码

XML configuration file parsing and modeling

fiddler-AutoResponder

STM32基础知识—内存映射
![[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)](/img/94/b4df1ce2861a851fcd8de3e08540b0.png)
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)

Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
随机推荐
The landing practice of ByteDance kitex in SEMA e-commerce scene
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
【华为机试真题详解】高矮个子排队
ORM--分组查询,聚合查询,查询集QuerySet对象特性
Guide de signature du Code Appx
嵌入式工程师如何提高工作效率
STM32中AHB总线_APB2总线_APB1总线这些是什么
Postman interface test V
XML configuration file parsing and modeling
Guid primary key
STM32 ADC和DMA
Appx代碼簽名指南
SolidWorks工程图中添加中心线和中心符号线的办法
Appx代码签名指南
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Remote meter reading, switching on and off operation command
2022.7.4DAY596
高数_第1章空间解析几何与向量代数_向量的数量积
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors