What are the Operators that Can be and Cannot be Overloaded in C++?

27 May, 2022
There are various ways to overload Operators in C++ by implementing any of the following types of functions:
1) Member Function
2) Non-Member Function
3) Friend Function
List of operators that can be overloaded are: 
+
–
 *
⁄
%
‸
 &
|
 ~
!
=
 <
>
+=
-=
 *=
⁄=
%=
‸=
&=
|=
 <<
 >>
<<=
>>=
==
 !=
<=
>=
&&
||
 ++
 —
,
->*
->
( )
[ ]
new
delete
new[]
delete[]
 
Example 1: Overloading ++ Operator 
CPP
// CPP program to illustrate// operators that can be overloaded#include <iostream>using namespace std; class overload {private:    int count; public:    overload()        : count(4)    {    }     void operator++() { count = count + 1; }    void Display() { cout << "Count: " << count; }}; int main(){    overload i;    // this calls "function void operator ++()" function    ++i;    i.Display();    return 0;}
Count: 5
This function is called when the ++ operator operates on the object of the 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 i.e. pre and post increment operator 
CPP
// CPP program to demonstrate the// Difference between pre increment// and post increment overload operator#include <iostream>using namespace std; class overload {private:    int count; public:    overload(int i)        : count(i)    {    }     overload operator++(int) { return (count++); }    overload operator++()    {        count = count + 1;        return count;    }    void Display() { cout << "Count: " << count << endl; }};// Driver codeint main(){    overload i(5);    overload post(5);    overload pre(5);     // this calls "function overload operator ++()" function    pre = ++i;    cout << "results of I   =   ";    i.Display();    cout << "results of preincrement   =  ";    pre.Display();    // this call "function overload operator ++()"function    i++; // just to show diff    i++; // just to show diff    post = i++;    cout << "Results of post increment   =   ";    post.Display();    cout << "And results of i , here we see difference   : "            "  ";    i.Display();    return 0;}
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 [ ] operator 
CPP
// CPP program to illustrate overloading the// [ ] operator #include <iostream>using namespace std;class overload {    int a[3]; public:    overload(int i, int j, int k)    {        a[0] = i;        a[1] = j;        a[2] = k;    }    int operator[](int i) { return a[i]; }};int main(){    overload ob(1, 2, 3);    cout << ob[1]; // displays 2    return (0);}
2
Example 4 : Overloading -> operator 
CPP
// CPP program to illustrate// operators that can be overloaded#include <bits/stdc++.h>using namespace std; class GFG {public:    int num;    GFG(int j) { num = j; }    GFG* operator->(void) { return this; }}; // Driver codeint main(){    GFG T(5);    GFG* Ptr = &T;     // Accessing num normally    cout << "T.num = " << T.num << endl;     // Accessing num using normal object pointer    cout << "Ptr->num = " << Ptr->num << endl;     // Accessing num using -> operator    cout << "T->num = " << T->num << endl;     return 0;}
T.num = 5
Ptr->num = 5
T->num = 5
List of operators that cannot be overloaded
1) Scope Resolution Operator  (::)    
2) Ternary or Conditional Operator (?:)   
3) Member Access or Dot operator  (.)    
4) Pointer-to-member Operator (.*)  
5) Object size Operator (sizeof) 
6) Object type Operator(typeid) 
7) static_cast (casting operator)
8) const_cast (casting operator)
9) reinterpret_cast (casting operator)
10) dynamic_cast (casting operator)
Example 5: Overloading this .(dot) operatorDot (.) operator can’t be overloaded, so it will generate an error. 
CPP
// C++ program to illustrate// Overloading this .(dot) operator#include <iostream>using namespace std; class cantover {public:    void fun();};class X {    cantover* p;    cantover& operator.() { return *p; }    void fun();};void g(X& x){    x.fun(); // X::fun or cantover::fun or error?}
Output: Error
prog.cpp:12:23: error: expected type-specifier before ‘.’ token
    cantover& operator.() { return *p; }
This program will generate an error. Similarly, the above operators will also generate an error, if overloaded.
This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.
solankigautamj6789
Akanksha_Rai
anshikajain26
chandramauliguptach
C Language
C++
GFacts
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Multidimensional Arrays in C / C++
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++