In C++, following are the general rules for operator overloading.
1) Only built-in operators can be overloaded. New operators can not be created.
2) Arity of the operators cannot be changed.
3) Precedence and associativity of the operators cannot be changed.
4) Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.
5) Operators cannot be overloaded for built in types only. At least one operand must be used defined type.
6) Assignment (=), subscript (), function call (“()”), and member selection (->) operators must be defined as member functions
7) Except the operators specified in point 6, all other operators can be either member functions or a non member functions.
8 ) Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Operands for sizeof operator
- Copy constructor vs assignment operator in C++
- Self assignment check in assignment operator
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- C/C++ Ternary Operator - Some Interesting Observations
- Function overloading and return type
- Is assignment operator inherited?
- Function overloading and const keyword
- When should we write our own assignment operator in C++?
- Default Assignment Operator and References
- Does overloading work with Inheritance?
- Can we use % operator on floating point numbers?
- Function Overloading and float in C++
- Scope Resolution Operator Versus this pointer in C++?