About overloading of assignment operator "="

Overloading of the assignment operator should be done in this way:

Boy& operator=(const Boy &boy);

Note: Parameters should use references!

The correct complete code is:
Boy class (in the file: "Boy.h"):

#include <string>

using namespace std;

class Boy{
    
    
public:
	Boy(const char *name = NULL);
	~Boy();
	Boy& operator=(const Boy& boy);

	string getName();
private:
	char *name;
};

The implementation of the Boy class (in the file: Boy.cpp):

#include "Boy.h"

Boy::Boy(const char *name) {
    
    
	//判断传递的是否为空指针,是空指针就是没有命名
	if(!name) {
    
    
		name = "未命名";
	}
	this->name = new char[strlen(name) + 1];
	strcpy_s(this->name, strlen(name) + 1, name);
}

Boy& Boy::operator=(const Boy& boy) {
    
    
	if(name) delete name;
	this->name = new char[strlen(boy.name) + 1];
	strcpy_s(this->name, strlen(name) + 1, boy.name);
	return *this;
}

string Boy::getName() {
    
    
	return name;
}

Boy::~Boy() {
    
    
	if(name) delete[] name;
}

Main function entry point:

#include <iostream>
#include "Boy.h"

int main(void) {
    
    
	Boy boy1("大鱼");
	Boy boy2, boy3;
	
	boy3 = boy2 = boy1;
	cout << boy1.getName() << endl;
	cout << boy2.getName() << endl;
	cout << boy3.getName() << endl;

	return 0;
}

Correct compilation result:
insert image description here
Error 1:
If defined as:

Boy& operator=(const Boy *boy);

Defined in this way, the function will not work, and the compiler will not recognize it as an overload of the assignment operator, that is, this function will not be called when boy2 = boy1.
At this time, the assignment constructor automatically generated by the compiler is called, but the program will crash at the end!
The reason is that the assignment constructor automatically generated by the compiler performs a shallow copy, copies the value of the pointer name, and does not re-allocate space for name, resulting in the data name in the three objects all using the same space, the destructor is called three times, the same data name is released three times, and the program crashes! ! !

Mistake 2:
If you define:

Boy& operator=(const Boy boy);

It works, but when calling, it will execute parameter passing:
that is, when boy2 = boy1; is executed, it will execute: boy2.operator=(boy);
However, when boy2.operator=(boy); is executed, it will execute: const Boy boy = boy1;
execute const Boy boy = boy1;, and the copy constructor of the Boy class will be executed!
However, no copy constructor is defined. Then, the compiler will automatically generate a copy constructor for shallow copying!

Shallow copy is to directly copy the value of the pointer variable boy1.name to the pointer boy.name, causing the two pointers to point to the same address, which is very dangerous! Since the variable boy is a temporary variable, it will die after this function is executed! The demise will call the destructor to release the memory. At this time, the values ​​pointed to by boy1.name and boy.name are random and messy data, as follows: Define the assignment constructor like this: Boy& operator=(const Boy boy), which has two effects: 1) Waste performance 2) If there is no custom copy constructor, and this class has pointer members, the automatically generated copy constructor will be called, resulting in shallow copy. Corruption
insert image description here
or
program
crash
!

Summary:
1) To overload the assignment operator, you must use reference parameters
2) If a class has pointer members and uses dynamic memory allocation, then you must define your own copy constructor [to use deep copy], avoid calling the automatically generated copy constructor because the automatically generated copy constructor is a shallow copy
!

Guess you like

Origin blog.csdn.net/weixin_46060711/article/details/124358415