Operator overloading in C++ is the ability to give new meaning to existing operators. This can be useful for making code more concise and readable, or for providing new functionality to user-defined types.
To overload an operator, you need to define a function with the same name as the operator, and the same number and types of arguments. The function body will contain the code that you want to execute when the operator is used.
For example, the following code overloads the + operator for the
Point class:
C++
class Point {
public:
int x;
int y;
Point(int x, int y) {
this->x = x;
this->y = y;
}
Point operator+(const Point& other) {
return Point(this->x + other.x, this->y + other.y);
}
};
int main() {
Point p1(10, 20);
Point p2(30, 40);
Point p3 = p1 + p2;
cout << p3.x << ", " << p3.y << endl; // 40, 60
return 0;
}
In this code, the + operator is overloaded to add two Point objects together. The
operator+() function takes two Point objects as arguments, and returns a new
Point object that is the sum of the two original objects.
When the + operator is used in the main() function, the compiler will call the
operator+() function instead of the built-in + operator. This will cause the code to add the two
Point objects together and return the result.
Here are some of the rules for operator overloading in C++:
Only built-in operators can be overloaded.
The arity of the operator cannot be changed.
The precedence of the operator remains the same.
The overloaded operator cannot hold the default parameters except function call operator “()”.
Operator overloading can be a powerful tool for making your code more concise and readable. However, it is important to use it carefully, as it can also make your code less readable and more difficult to debug.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Operator overloading in C++ is the ability to give new meaning to existing operators. This can be useful for making code more concise and readable, or for providing new functionality to user-defined types.
To overload an operator, you need to define a function with the same name as the operator, and the same number and types of arguments. The function body will contain the code that you want to execute when the operator is used.
For example, the following code overloads the
+operator for thePointclass:C++
In this code, the
+operator is overloaded to add twoPointobjects together. Theoperator+()function takes twoPointobjects as arguments, and returns a newPointobject that is the sum of the two original objects.When the
+operator is used in the main() function, the compiler will call theoperator+()function instead of the built-in+operator. This will cause the code to add the twoPointobjects together and return the result.Here are some of the rules for operator overloading in C++:
Operator overloading can be a powerful tool for making your code more concise and readable. However, it is important to use it carefully, as it can also make your code less readable and more difficult to debug.