The Wayback Machine - https://web.archive.org/web/20240806105205/https://www.geeksforgeeks.org/what-are-the-operators-that-can-be-and-cannot-be-overloaded-in-cpp/
Open In App

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

Last Updated : 27 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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


Output

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 code
int 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;
}


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


Output

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 code
int 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;
}


Output

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) operator
Dot (.) 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.



Previous Article
Next Article

Similar Reads

Functions that cannot be overloaded in C++
In C++, following function declarations cannot be overloaded. 1) Function declarations that differ only in the return type. For example, the following program fails in compilation. C/C++ Code #include&amp;lt;iostream&amp;gt; int foo() { return 10; } char foo() { return 'a'; } int main() { char x = foo(); getchar(); return 0; } 2) Member function de
3 min read
Can main() be overloaded in C++?
Predict the output of following C++ program. #include &lt;iostream&gt; using namespace std; int main(int a) { cout &lt;&lt; a &lt;&lt; &quot;\n&quot;; return 0; } int main(char *a) { cout &lt;&lt; a &lt;&lt; endl; return 0; } int main(int a, int b) { cout &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt; b; return 0; } int main() { main(3); main(&quot;C++
2 min read
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th
9 min read
Hiding of all Overloaded Methods with Same Name in Base Class in C++
In C++, function overloading is possible i.e., two or more functions from the same class can have the same name but different parameters. However, if a derived class redefines the base class member method then all the base class methods with the same name become hidden in the derived class. For example, the following program doesn't compile. Here,
3 min read
What are the differences between bitwise and logical AND operators in C/C++?
A Bitwise And operator is represented as '&amp;' and a logical operator is represented as '&amp;&amp;'. The following are some basic differences between the two operators. a) The logical and operator '&amp;&amp;' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. The bitwise and operator '&amp;' work on Inte
4 min read
Increment and Decrement Operators in C
The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1 respectively. The incrementation and decrementation are one of the most frequently used operations in programming for looping, array traversal, pointer arithmetic, and many more. In this article, we will discuss the
4 min read
C++ Increment and Decrement Operators
Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There are two types of C++ increment Operator: Pre-Increm
4 min read
G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
Most of the languages including C, C++, Java, and Python provide a boolean type that can be either set to False or True. In this article, we will see about logical and bitwise not operators on boolean. Logical Not (or !) Operator in PythonIn Python, the logical not operator is used to invert the truth value of a Boolean expression, returning True i
4 min read
Conditionally assign a value without using conditional and arithmetic operators
Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked: If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'. Note: - You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator (+, -, *, /)
6 min read
new and delete Operators in C++ For Dynamic Memory
Dynamic memory allocation in C/C++ refers to performing memory allocation manually by a programmer. Dynamically allocated memory is allocated on Heap, and non-static and local variables get memory allocated on Stack (Refer to Memory Layout C Programs for details). What are applications?  One use of dynamically allocated memory is to allocate memory
6 min read
Practice Tags :
three90RightbarBannerImg