operators overloading
- Notice
- Not all operators can be overloaded; overloaded operators cannot change their priority or the number of their operands.Except for'()', any overloadable operator can set default values.
- Note that the c++ operator overload parameter must contain at least one class or struct, enum, union, or at least one type that is not built-in (built-in types such as int, char, and so on).
- Operator plus operator to overload is equivalent to a function, such as "operator == ()", which may belong to a class or may be a global method.
Operator overload example
-
Global operator overload
-
A simple example
#include <stdio.h> #include <stdlib.h> class CPerson { public: int age; CPerson(int age); }; CPerson::CPerson(int age) { this -> age = age; } void operator + (CPerson cObject, int b) { printf("xiaoming + int\r\n"); } void operator + (CPerson cObject, float b) { printf("xiaoming + float\r\n"); } void operator + (CPerson cObject, char b) { printf("xiaoming + char\r\n"); } int main(void) { CPerson xiaoming(5); xiaoming + 2; xiaoming + 2.0f; xiaoming + '2'; system("pause>nil"); exit(0); }
The output is:
Summary: You can think of the operator keyword and the overloaded operator as the function name of a function.
-
A deeper exploration
#include <stdio.h> #include <stdlib.h> class CPerson { public: int age; CPerson(int age); }; CPerson::CPerson(int age) { this -> age = age; } //+overloaded function 1 int operator + (const CPerson &cObject1, const CPerson &cObject2) //Global operator overload function { //operator +, as a whole, corresponds to the function name of a function. return cObject1.age + cObject2.age; } //+overloaded function 2 int operator + (int number, const CPerson &cObject1) { return number + cObject1.age; } int operator + (const CPerson &cObject1, int number) { return cObject1.age + number; } //+overloaded function 3 //int operator + (int number1, int number2)//Error: At least one of the overloaded operator types must be non-built-in //{// Non-built-in types are: class, struct, enum, union // return number1 + number2 + 1; //} // Overloaded function 4 //Int operator. (CPerson cObject1, int age) //Error:'. 'operator is not allowed to be overloaded //Only five operators in {//C++ that do not allow overloading are:. *:: sizeof?: // return cObject1.age + 1;. //} // Overloaded function 5 //Int operator + (const CPerson &cObject1) //Number of operands cannot be overloaded //{ // return cObject1.age + 3; //} int main(void) { CPerson xiaoming(5); CPerson xiaohong(7); printf("xiaoming + xiaohong = %d\r\n", xiaoming + xiaohong); printf("3 + xiaoming = %d\r\n", 3 + xiaoming); //Note: The Addition Exchange Law is still in effect printf("xiaoming + 3 = %d\r\n", xiaoming + 3); //That is, xiaoming + int = int + xiaoming, and I only defined int + Xiaoming //Although the operation of int + xiaoming is defined, and the operation of xiaoming + int is not defined, the default Addition Exchange Law property is assumed when invoked, but the definition of xiaoming + float is assumed //At this point, instead of defaulting to the law of addition and exchange, the call to xiaoming + float, even if it is an int, does not conform to float. /*----------Demonstration of Errors-----------*/ //printf("3 + 3 = %d\r\n", 3 + 3); //printf("xiaoming.age = %d\r\n", xiaoming.age); //printf("xiaoming.age + = %d\r\n", xiaoming.age +); system("pause>nil"); exit(0); }
The output is:
Summary: At least one of the operands of an overloaded operator must be of a non-built-in type; the priority of the operator cannot be changed, and the number of operands of the operator cannot be changed; some of the original operating characteristics of the operation still exist, such as the law of addition and exchange; not all operators can be overloaded, and there are five operators that cannot be overloaded:
Symbol Symbol Interpretation . member access operator .* Member Pointer Access Operator :: Field Operators sizeof length operator ?: Conditional operator -
-
Intra-class operator overload
#include <stdio.h> #include <stdlib.h> class CDog { public: CDog(int age); int age; }; CDog::CDog(int age) { this->age = age; } class CPerson { public: int age; CPerson(int age); int operator + (const CPerson &cObject2); int operator + (int a); float operator + (float a); }; CPerson::CPerson(int age) { this -> age = age; } int CPerson::operator + (const CPerson &cObject2) //Global operator overload function { //operator +, as a whole, corresponds to the function name of a function. return this -> age + cObject2.age; } int CPerson::operator + (int a) { return this -> age + a; } float CPerson::operator + (float a) { return this -> age + a; } int main(void) { CPerson xiaoming(5); CPerson xiaohong(7); CDog wangcai(2); printf("xiaoming + xiaohong = %d\r\n", xiaoming + xiaohong); printf("xiaoming + int = %d\r\n", xiaoming + 3); printf("xiaoming + float = %.3f\r\n", xiaoming + 3.0f); /*----------------Error demonstration-----------------*/ //Printf ("xiaoming + int =%d\r\n", 3 + xiaoming); //For operator overload within a class, the first operator on the left must be an object of that class and no longer have the law of addition and exchange //Printf ("wangcai + int =%d\r\n", Wangcai + 3); //The overloaded operator within the class does not belong to the CDog class, so the CDog object cannot be invoked. system("pause>nil"); exit(0); }
The output is:
Summary: Operator overloaded methods within a class can only be invoked with objects of that class by using the object of that class as the first operand from the left.