Open In App

Rules for operator overloading

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In C++, following are the general rules for the things that are not allowed with 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 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.

Following are the general rules of the operator overloading.

  1. Syntax/Declaration Rule: While overloading a function we need to use the keyword operator , proceeded by class name and followed by operator symbol such as ‘+’.
  2. Member and Non-member functions: We can overload operators in two ways:- first is as a member functions , second is as a non-member functions. When overloading as a member function, the left operand represents the object on which the function is called and when overloading as a non-member function, we can specify any type for the left operand.
  3. Number of operands Required: Most operators can be overloaded with one or two operands. Example- Unary operators like ++ require one operand, while binary operators like + require two operands.
  4. Precedence and associativity: Operator precedence and associativity cannot be changed when overloading operators. Example- It’s not allowed to change the precedence of (*) or the associativity of (/).
  5. Return type: When we overload operators than we must define the return type of the overloaded operator function according to our need.
  6. Friend function: If overloaded operators needs to access private member variables/functions of a class than they must be declared as a friend function.
  7. Special Syntax: For operators like (), [], or -> we must use a special syntax when overloading. Example, in order to overload the [] operator and to make objects callable like functions, we need to define a function named operator().

C++




#include <bits/stdc++.h>
using namespace std;
 
class Increment {
private:
    int cnt;
 
public:
    Increment()
        : cnt(0)
    {
    }
 
    // Overloading the post-increment operator ++
    Increment operator++(int)
    {
        Increment dp(*this);
        cnt++;
        return dp;
    }
    int get() const { return cnt; }
};
 
int main()
{
    Increment i1;
 
    cout << "Before Increment- " << i1.get() << "\n";
 
    Increment i2 = i1++; // Post-incrementing i1
 
    cout << "After Increment- " << i1.get() << "\n";
    cout << "i2 is- " << i2.get() << "\n";
 
    return 0;
}


Output

Before Increment- 0
After Increment- 1
i2 is- 0



Last Updated : 05 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads