C++ assignment operator function

Topic: Given the following declaration that the type is CMyString, please add an assignment operator function for this type.

class CMyString
{
    
    
public:
	CMyString(char* pData = NULL);
	CMyString(const CMyString& str);
	~CMyString(void);
private:
	char* m_pData;
};

Interviewer concerns:

1. Whether to declare the type of the return value as a reference of this type, and return the reference of the instance itself (* this ) before the end of the function . Only when a reference is returned can continuous assignment be performed. Otherwise, if the function return value is void, continuous assignment will not be possible using the assignment function. For example: there are three objects: str1, str2, str3, str1=str2=str3 such assignment statement will be wrong.
2. Whether to declare the type of the passed parameter as a constant reference. Formal parameters to actual parameters will call the copy constructor, and declaring the parameters as references can avoid memory consumption and improve code execution efficiency. At the same time, the attribute of the passed instance will not be changed in the assignment operator, so the ( const ) keyword should be added before the parameter .
3. Whether the memory of the instance itself. If the memory of the instance is not released after the program ends, it will cause a memory leak.
4. Whether to judge whether the passed parameter and the current instance (* this ) are the same. If it is the same, it does not need to be assigned and returns directly. When (* this ) and the passed-in parameter are the same instance, once the memory of its own is released, the memory of the passed-in parameter will also be released, and the content that needs to be assigned cannot be found.

Based on the above requirements, the following code can be written:

CMyString& CMyString::operator=(const CMyString& str)
{
    
    
	//检查自赋值
	if(this == &str)
	{
    
    	
		return *this;
	}
	//释放原资源
	delete [] m_pData;
	//分配新内存
	m_pData = new char(strlen(str.m_pData) + 1);
	strcpy_s(m_pData, strlen(str.m_pData) + 1, str.m_pData);
	//返回引用
	return *this;
}

It can be seen that the memory of pData is released with delete before the memory is allocated. If there is insufficient memory at this time and cause new char to throw an exception,
pData will be a null pointer, which will easily cause the program to crash.

So there are the following improvements:

CMyString& CMyString::operator=(const CMyString& str)
{
    
    
	if (this != &str)		//检查自赋值
	{
    
    
		CMyString tmp(str);	//拷贝构造临时变量
		char* p = tmp.m_pData;//交换对象与临时对象的数据
		tmp.m_pData = this->m_pData;
		this->m_pData = p;
	}
	return *this;
}

In this function, we first create a temporary instance tmp, and then
exchange tmp.m_pData with the m_pData of the instance itself. Since tmp is a local variable, but the scope of the variable is out of scope when the program runs outside the if, it will automatically call
the destructor of tmp to release the memory pointed to by tmp.m_pData. Since
the memory pointed to by tmp.m_pData is the memory of mpData before the instance, this is equivalent to automatically calling the destructor to release the memory of the instance

Test:
Add the Show function to the class to display the current value of each object

void Show()	//测试
	{
    
    
		if (m_pData == nullptr)	cout << "NULL" <<endl;
		else
			cout << this->m_pData << endl;
	}

main function:

int main()
{
    
    
	char str[] = "Hello C++";

	//实例化三个对象
	CMyString str1;
	CMyString str2;
	CMyString str3;

	str1 = str;	//赋值
	cout << "str1是: " ;
	str1.Show();

	str1 = str1;//自赋值
	cout << "str1是: ";
	str1.Show();

	str3 = str2 = str1;//连续赋值
	cout << "str2是: ";
	str2.Show();
	cout << "str3是: ";
	str3.Show();

	return 0;
}

operation result:

Insert picture description here

Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/103050369