What are the operators that can be and cannot be overloaded in C++?

1.7

List of operators that can be overloaded are:

+    -    *    /      %        ^
&    |    ~    !,        =
    =      ++        --
<>    ==    !=      &&        ||
+=    -=    /=    %=      ^=        &=
|=    *=    <>=      []        ()
->    ->*    new    new []      delete    delete []

Example 1 : Overloading ++ operator

// 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;
}

Output:

Count: 5

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 this [ ] 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);
}

Output:

2

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: Overloading this .(dot) operator

Dot (.) operator can’t be overloaded, so it cause error.

// C++ program to illustrate
// Overloading this .(dot) operator
#include <iostream>
#include <iostream>
class cantover {
public:
    void fun();
};
class X { // assume that you can overload .
    cantover* p;
    cantover& operator.()
    {
        return *p;
    }
    void fun();
};
void g(X& x)
{
    x.fun(); // X::fun or cantover::fun or error?
}

Output

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 contribute@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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 25 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.