[C++] The characteristics and format of operator overloading and assignment operator overloading (explicit and default), as well as pre-++ and post-++ overloading


foreword

`

C++ introduces operator overloading to enhance the readability of the code. Operator overloading is a function with a special function name, and also has its return value type, function name, and parameter list. The return value type and parameter list are similar to ordinary functions.

1. Operator overloading

The function name is: the keyword operator followed by the operator symbol that needs to be overloaded.
Function prototype: return value type operator operator (parameter list)

Note:
1... You cannot create a new operator by connecting other symbols: such as operator@
2. The overloaded operator must have a class type parameter
3. The meaning of the operator used for the built-in type cannot be changed, for example: built-in Integer +, cannot change its meaning When overloaded
as a member function of a class , its formal parameter seems to be 1 less than the number of operands , because the first parameter of the member function is the hidden this 4 . . :: sizeof ?: . * Note The above 5 operators cannot be overloaded. This often appears in written multiple choice questions.

Global operator<:
insert image description here
So we generally don’t write the operator overloaded function as a global function, but write it in the class as a member function.
Here I write all the comparisons:

insert image description here
insert image description here

2. Assignment operator overloading

Date&operator=(const Date& x) {
    
    
		//赋值重载函数
		if ( this != &x) {
    
    
			_year = x. _year;
			_month = x._month;
			_day = x._day;
		}
		return *this;
	}

1. Assignment operator overload format:

1. Parameter type: const T&, passing reference can improve the efficiency of parameter passing
2. Return value type: T&, returning reference can improve the efficiency of returning, and the purpose of returning value is to support continuous assignment.

insert image description here
insert image description here
When we perform continuous assignment, if it is void The type will report an error, because d2 does become the value of d3 after assignment, but the overall level of d2=d3 and the return value of d2.operator (d3) is void and assigned to d1 will report an error

3. Check whether you assign a value to yourself >
insert image description here

4. Return *this: to compound the meaning of continuous assignment

2. When the user does not explicitly implement, the compiler will generate a default assignment operator overload, which is copied byte by byte in the form of value.

Note: Built-in type member variables are directly assigned, while custom type member variables need to call the assignment operator overload of the corresponding class to complete the assignment.

By default, the behavior of assignment overload and copy construction is the same:
1. Built-in type members-value copy/shallow copy
2. Custom type members: will call their assignment overload
3. When assignment involves memory release and the like, we You have to implement the deep copy yourself, otherwise it is easy to make mistakes, and in other cases, you can let the compiler write it for us.
insert image description here

3. Assignment operators can only be overloaded as member functions of classes and cannot be overloaded as global functions

If there are no members in a class, it is simply called an empty class.
Is there really nothing in the empty class? No, when any class does not write anything, the compiler will automatically generate the following 6 default member functions.
Default member function: The member function generated by the compiler without explicit implementation by the user is called the default member function.

insert image description here
The assignment overloading function is also among these six functions. When we define the assignment overloading function as a global function, the compiler will think that we have not written an assignment overloading function in our class, and it will generate one for us. This is It contradicts the assignment overload function we defined in the global.

3. Front ++ and post ++

insert image description here

1. There is already a copy between two objects: operator overloading function
2. Use an existing object to initialize another object: constructor

Guess you like

Origin blog.csdn.net/m0_74774759/article/details/130777917