List of operators that can be overloaded are:
+ - * / % ^ & | ~ !, = = ++ -- == != && || += -= /= %= ^= &= |= *= =  () -> ->* new new  delete delete 
Example 1 : Overloading ++ operator
This function is called when ++ operator operates on the object of overload class (object i in this case). In the program, void operator ++ () operator function is defined (inside overload class). This function increments the value of count by 1 for i object.
Example 2 : Overloading ++ operator and overloading postincrement operator
output: results of I = Count: 6 results of preincrement = Count: 6 Results of post increment = Count: 8 And results of i , here we see difference : Count: 9
Example 3: Overloading this [ ] operator
Example 4 : Overloading -> operator
T.num = 5 Ptr->num = 5 T->num = 5
List of operators that cannot be overloaded
1> Scope Resolution Operator (::) 2> Pointer-to-member Operator (.*) 3> Member Access or Dot operator (.) 4> Ternary or Conditional Operator (?:) 5> Object size Operator (sizeof) 6> Object type Operator (typeid)
Example 5 : Overloading this .(dot) operator
Dot (.) operator can’t be overloaded, so it cause error.
prog.cpp:8:27: error: expected type-specifier before '.' token cantover& operator.() ^ prog.cpp:8:19: error: expected ';' at end of member declaration cantover& operator.() ^ prog.cpp:8:27: error: expected unqualified-id before '.' token cantover& operator.() ^ prog.cpp: In function 'void g(X&)': prog.cpp:13:14: error: 'void X::fun()' is private void fun(); ^ prog.cpp:18:15: error: within this context x.fun(); // X::fun or cantover::fun or error? ^
This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best.
This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Functions that cannot be overloaded in C++
- Can main() be overloaded in C++?
- Hiding of all overloaded methods with same name in base class
- Operators in C | Set 2 (Relational and Logical Operators)
- Operators in C | Set 1 (Arithmetic Operators)
- # and ## Operators in C
- Operators in C / C++
- unordered_set operators in C++ STL
- Assignment Operators in C/C++
- C | Operators | Question 22
- C | Operators | Question 21
- C | Operators | Question 27
- C | Operators | Question 26
- C | Operators | Question 18
- C | Operators | Question 17
- Bitwise Operators in C/C++
- C | Operators | Question 23
- C | Operators | Question 24
- Unary operators in C/C++
- C | Operators | Question 3