[C++] Operator overloading ② (Cloud operator overloading defined inside the class - member function | Operator overloading defined outside the class - global function | overloadable operator)





1. The nature of operator overloading



The essence of operator overloading is "function call";

When using + will be 2 2When adding two objects, the C++ compiler will look for whether there is an operator overloaded function defined;

	// 自定义类型相加
	Student s1(10, 120), s2(18, 170);
	Student s3, s4, s5;

	// 运算符重载
	s5 = s1 + s2;

If an operator overloaded function is found, the contents of the function will be executed, replacing 2 2Two objects perform an addition operation. If no operator overloaded function is found, an error will be reported;

The error message is as follows:

error C2676: 二进制“+:“Student”不定义该运算符或到预定义运算符可接收的类型的转换
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。




2. Operator overloading syntax - cloud operator overloading (member function) defined inside the class




1. Operator overloading function syntax description


C++ allows redefining the behavior of operators, such as commonly used mature operators for addition and subtraction, which can be overloaded; the operations of operators can be customized;

The cloud operator overload is defined inside the class , the format is " return value type (class name) operator operator symbol (const parameter type name & parameter variable name) {method content}" , the type of the parameter is a reference type ;

Addition operator overloading , overloading the "+" operator, is to add the number member variables of the two Operators, and then return a new Operator object, whose number member variable value is the number member variable of the two Operators. sum of values;

//运算符重载 , "+" 号运算符进行重载 , 
//其作用是让两个 Operator 的 number 成员变量相加
//运算符重载的本质是按照一定格式定义一个方法
//这个定义的方法中包含运算符 , 除运算符之外的其它符号可以省略简写
public:
	Operator operator+(const Operator& o1) {
    
    
		//+ 运算符的作用是 两个 Operator 对象, 进行操作得到第三个 Operator 对象
		//第三个 Operator 对象的 number 变量 , 是前两个 Operator 对象之和
		Operator o2;
		o2.number = this->number + o1.number;
		return o2;
	}

The essence of operator overloading is to define a method. The method has a fixed format to define. When calling the method, you can call it in function form or use operators to perform calculations. Its essence is still a function call of the class ;


2. Operator overloading function call


The overloaded operator is called completely , that is, the entire method defined above is called operator+, which is a formal function calling method;

	//这是运算符重载的完整写法 , 
	//其中的 .operator 和之后的 () 可以省略变成下面的简化写法
	Operator o3 = o1.operator+(o2);
	//打印 o3 中的 number 变量值
	cout << "内部定义的运算符重载完整写法结果 : " << o3.number << endl;

Operator overloading simplifies calls (recommended) . This kind of call is operator operation, and the writing method is "object 1 operator object 2" . The result is object 3; the difference between this calling method and the above is that it omits the .operatorand parameters outside the call. brackets ();

	//+ 是在 Operator 类中自定义的运算符重载 
	//其作用是返回一个对象 , 其number成员变量值是 o1 和 o2 中number成员变量之和
	Operator o4 = o1 + o2;
	//打印 o3 中的 number 变量值
	cout << "内部定义的运算符重载简化写法结果 : " << o4.number << endl;

3. Code example - operator overloaded function call


Operator overloading call complete code:

	//运算符重载
	//注意这里的 Operator 对象 o1 和 o2 都在栈内存中
	Operator o1;
	o1.number = 80;

	Operator o2;
	o2.number = 10;

	//运算符重载完整写法

	//这是运算符重载的完整写法 , 
	//其中的 .operator 和之后的 () 可以省略变成下面的简化写法
	Operator o3 = o1.operator+(o2);
	//打印 o3 中的 number 变量值
	cout << "内部定义的运算符重载完整写法结果 : " << o3.number << endl;

	//运算符重载简化写法

	//+ 是在 Operator 类中自定义的运算符重载 
	//其作用是返回一个对象 , 其number成员变量值是 o1 和 o2 中number成员变量之和
	Operator o4 = o1 + o2;
	//打印 o3 中的 number 变量值
	cout << "内部定义的运算符重载简化写法结果 : " << o4.number << endl;

Code execution result:

内部定义的运算符重载完整写法结果 : 90
内部定义的运算符重载简化写法结果 : 90




3. Operator overloading syntax - operator overloading defined outside the class (global function)




1. Operator overloading function syntax description


Operator overloading is defined outside the class . Operator overloading can also be defined outside the class. It can be in any code that contains the class header file. The definition method is compared with the definition inside the class. Only the parameters are different. They are defined outside the class . Two parameters are required, representing the two parameters of the operator operation;

Multiplication operator overloading , the "*" operator is overloaded, its function is to multiply the number member variables of the two Operators, and then return a new Operator object, whose number member variable value is the number member variable of the two Operators product of values;

//类外部定义云算符重载
//	使用该重载云算符时 , 将两个对象相乘 , 获得的第三个对象 , 
//	该对象的 number 成员变量值 , 是 前两个对象的 number 对象的乘积 
Operator operator*(const Operator& o1, const Operator& o2) {
    
    
	//+ 运算符的作用是 两个 Operator 对象, 进行操作得到第三个 Operator 对象
	//第三个 Operator 对象的 number 变量 , 是前两个 Operator 对象之和
	Operator o3;
	o3.number = o1.number * o2.number;
	return o3;
}

2. Operator overloading function call


To call an overloaded operator , you can directly call the operator overloaded operator*()method, or you can directly use the operator, o1 * o2;


	//运算符重载
	//注意这里的 Operator 对象 o1 和 o2 都在栈内存中
	Operator o1;
	o1.number = 80;

	Operator o2;
	o2.number = 10;

	//这是运算符重载的完整写法 , 
	//其中的 .operator 和之后的 () 可以省略变成下面的简化写法
	Operator o5 = operator*(o1, o2);
	//打印 o5 中的 number 变量值
	cout << "外部定义的运算符重载完整写法结果 : " << o5.number << endl;

	//运算符重载简化写法

	//+ 是在 Operator 类中自定义的运算符重载 
	//其作用是返回一个对象 , 其number成员变量值是 o1 和 o2 中number成员变量之积
	Operator o6 = o1 * o2;
	//打印 o6 中的 number 变量值
	cout << "外部定义的运算符重载简化写法结果 : " << o6.number << endl;

Code execution results

外部定义的运算符重载完整写法结果 : 800
外部定义的运算符重载简化写法结果 : 800




4. Overloadable operators



Here is a list of overloadable operators:

operator type List all overloadable operators of this type
Comparison operators (binary operators) == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)
Logical operators (binary operators) && (and), || (or), ! (not)
Numerical calculation operators (binary operators) + (addition), - (subtraction), * (multiplication), / (division)
Bit operators (binary operators) | (bitwise OR operation), & (bitwise AND operation), ~ (bitwise negation operation), ^ (bitwise XOR operation), << (left shift operation), >> (right shift operation)
Assignment operator (binary operator) = (equal), += (plus equal), -= (subtract equal), *= (multiply equal), /= (division equal), % = (modulo equal), &= (bitwise AND equal), | = (bitwise OR equal), ^= (bitwise XOR equal), <<= (left shift equal), >>= (right shift equal)
Unary operator + (positive number sign), - (negative number sign), * (pointer type), & (address operator), ++ (increment operator), – (decrement operator)
memory allocation release operator new (new object), new[] (new array object), delete (release object), delete[] (release array object)
function call operator ()
member access operator ->
subscript operator []
comma operator ,

Guess you like

Origin blog.csdn.net/han1202012/article/details/133513060