What is operator overloading
The so-called overloading is to give a new meaning again. Function overloading is to give a new meaning to an existing function to make it realize new functions. Therefore, a function name can be used to represent functions with different functions, that is, "one name is used for multiple purposes", and operators can also be overloaded. .
1 Why use the operator overloading mechanism
to use the complex class as an example
Complex c3 = c1 + c2;
the reason Complex is a user-defined type, the compiler does not know how to add or subtract
The compiler provides a mechanism for users to complete, Addition and subtraction operations of a custom type. . . . .
This mechanism is the operator overloading mechanism
2 The essence of operator overloading is a function
Operator overloading restrictions
The following operators cannot be overloaded
. :: .* ? : sizeof
Most operators can be overloaded except the above operators which cannot be overloaded. Overloaded operator functions can make new interpretations of operators, but the original basic semantics remain unchanged:
- Does not change the precedence of operators
- does not change the associativity of operators
- Does not change the operands required by the operator
- Cannot create new operator
Operator overloading basics
For example:
//Global function completion + operator overloading
Complex operator+(Complex &c1, Complex &c2)
//Class member function completion - operator overloading
Complex operator-(Complex &c2)
binary operator
When overloaded as a member function, it is interpreted as being called by the left operand, the left operand is passed through the this pointer, and the right operand is passed through the parameter.
When overloaded as a friend function, both left and right operands are passed as parameters.
unary operator
When overloaded as a member function, the operands are passed through the this pointer.
When overloaded as a friend function, the operands are passed as parameters.
Pre and Post Operator Summary
In C++, a placeholder parameter is used to distinguish the pre-operation and the post-operation, and the post-op operator has a place-holder parameter.
How to choose friend function and member function
When the class of the left operand cannot be modified, overloading with global functions
=, [ ] , ( ) and -> operators can only be overloaded with member functions
Overloading << >> instances with friend functions
class MyString
{
private:
char *m_data;
int m_len;
public:
MyString();
MyString(char *str);
//重载 << >>
friend ostream &operator <<(ostream &out, const MyString &s);
friend istream &operator >>(istream &in, MyString &s);
};
ostream &operator <<(ostream &out, const MyString &s)
{
out << s.m_data;
return out;
}
istream &operator >>(istream &in, MyString &s)
{
char str[1024] = {0};
in >> str;
if (s.m_data != NULL)
{
delete s.m_data;
}
s.m_len = strlen(str);
s.m_data = new char[s.m_len + 1];
strcpy(s.m_data, str);
return in;
}
Why not overload the && and | | operators
1) && and || are very special operators in C++
2) && and || built-in implement short-circuit rules
3) operator overloading is done by function overloading
4) operands are passed as function parameters
5) C++ functions The parameters will be evaluated, and the short-circuit rule cannot be implemented
Summarize
- Operator overloading is one of the powerful features of C++
- The essence of operator overloading is to extend the semantics of operators through functions
- The operator keyword is the key to operator overloading
- The friend keyword can develop access to functions or classes
- Operator overloading follows the rules of function overloading
- Operator overloading can be implemented directly using the member functions of the class
- The =, [], () and -> operators can only be overloaded through member functions
- The ++ operator is overloaded with prepending and trailing an int parameter
- Do not overload && and || operators in C++