C++ assignment operator overloading

  • 2020-11-03 22:32:11
  • OfStack

1. Reason for assignment operator overloading

The assignment operator is one of the most frequently used operations and usually has a meaning of 10, which is to transfer the values of two variables of the same type from end 1 (right end) to end 1 (left end). However, the assignment operator needs to be overloaded in two cases.
1 is an expression of a different type on both sides of the assignment sign and cannot be cast.
2 is the need for deep copy.

2. Considerations for assignment operator overloading

Assignment operators can only be overloaded in the form of member functions of a class. This means that if you want to pass the value of a user-defined type to a variable of a basic data type, you can do so only through a type conversion mechanism, not by overloading.

Assignment operators may need to be overridden when the expressions on either side of the assignment sign are not 1, as shown in the following example.


#include <iostream>
using namespace std;

class A
{
	int num;
public:
	A(){num=0;}
	A(int i){num=i;}
	void show(){
		cout<<num<<endl;
	}
};

int main(int argc, char* argv[])
{
	A a=5;		 // The data type on both sides of the symbol is not 1 Like, here means to create a new object 
	a.show();
	A a1;
	a1=1;    // The data types on both sides of the assignment sign are not 1 So, this is a real assignment 
	a1.show();  
}

The output result of the program is:

[

5
1

]

In the statement A a=5, although "=" is used, its semantics is to construct an object of class A a, which is equivalent to the statement A a(5), so the statement is independent of assignment. And statements a1 = 1 is a real assignment statement, is the type of the variable a1 A, and constant 1 type is int, because can kind of A constructor A int (int) will type into type A (1 class is actually a int parameters was constructed A temporary objects), and then complete the assignment operation, so don't have to overloaded assignment operator.

3. Overload the assignment operator in the case of deep copy

Deep copy is a factor in overloading assignment operators. So what is a deep copy? Simply put, deep copying refers to copying the contents of the region indicated by the pointer of a to the region indicated by the pointer of b if the object a contains non-dangling Pointers when copying one class object a to another object b. When making a deep copy, the 1 general object a and b have the same data type. If a deep copy occurs during an assignment, 1 must override the assignment operator, otherwise the assignment operator will follow the normal semantics of the assignment (passing data between member variables) without a deep copy. Consider the following example.


#include <iostream>
using namespace std;

class Student
{
	char* name;
	int age;
public:
	Student()
	{
		name=new char[20];
	}

	Student(char* n, int a)
	{
		name=new char[20];
		if(name) strcpy(name,n);
		age=a;
	}

	Student(const Student& s)
	{
		name=new char[20];
		*this=s;
	}
	
	void show()
	{
		cout<<"The student's name is "<<name;
		cout<<" and of age "<<age<<endl;
	}

	~Student()
	{
		delete[] name;
	}
	
	Student& operator=(const Student &s)
	{
		if(name) strcpy(name,s.name);
		age=s.age;
		return *this;
	}
};

int main()
{
	Student s1(" zhang 3",18),s4(" li 4",20);
	Student s2;
	s1.show();
	s2=s4;
	s2.show();
	Student s3=s1;
	s3.show();
	return 0;
}

The output result of the program is:

[

The student is 3 and of age 18
The student 's name is Lee 4 and of age 20
Sheet 3, and of age 18

]

Read the above program and note the following points.

(1) Since the pointer member name exists in class Student, a deep copy must be used when assigning values between two members of class Student. Perform s2 = s4; Statement to assign the s4 object to s2, where copying the contents of the es96EN4.name string into s2.name is the embodiment of the deep copy.

(2) Class copy constructors are not the same as assignment operators, but assignment operator overloading is usually used in copy constructors to avoid duplicate interpretation of the data passed between two objects.

(3) The above procedure should be used strcpy(name, ES106en.ES107en) directly; Implement data transfer of string members of two objects. This is a simplified approach with many pitfalls. For example, if the source string is longer than 20 characters, the program will get a run-time error. The solution is to reallocate the length of the destination string according to the length of the original string, and before that, to free the space of the destination string. In addition, if an object is assigned to itself, there will be a problem, and the address of the source object and the destination object need to be compared before the assignment is considered.

(4) Because deep copy will involve the dynamic allocation and release of memory, some more complex operations, so programmers should try to avoid the appearance of deep copy when writing custom classes. For example, in the above example, defining the member variable name as string name avoids writing your own code that implements a deep copy. The actual deep copy is done by the string class, which is provided by the C++ standard library and is safe to use.

(5) When the uppermost assignment operator is overridden, the return value of the operator function is usually defined as a reference to the assigned left operand type. This is for evaluating assignment expressions, and another is for chain operations.

That's C++ assignment operator overloading in detail, more on C++ assignment operator overloading in detail in other articles on this site!


Related articles: