In C++, we can overload the comma operator using Operator Overloading. For Example: For “Send the query X to the server Y and put the result in variable Z”, the “and” plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expression is expected. When the set of expressions has to be solved for operands, only the rightmost expression is considered.
Input: x = (y = 5, y + 2)
Output: x = 7, y = 5
In the above expression:
It would first assign the value 5 to y, and then assign y + 2 to variable x.
So, at the end ‘x’ would contain the value 7 while variable ‘y’ would contain value 7.
Operators Resistant to Overloading are as follows:
- Scope resolution operator(::)
- member selector(.)
- member pointer selector(*)
- ternary operator(?:)
return_type class_name::operator op(argument_list)
1) return_type: is the type of value returned by the function.
2) class_name: is the name of the class.
3) op: is an operator function where op is the operator being overloaded, and the operator is the keyword.
Rules for Operator Overloading:
- Existing operators can only be overloaded, but the new operators cannot be overloaded.
- The overloaded operator contains at least one operand of the user-defined data type.
- The friend function can’t be used to overload certain operators. However, the member function can be used to overload those operators.
- When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
- When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.
In the below code, although, each expression is evaluated by the compiler, the values of left-hand expression are discarded. Finally, The value of the right-hand operation is returned by the function. This triggers the overloaded comma operator to function similarly to its default operation.
x=10 y=20 x=5 y=30 x=1 y=1 x=10 y=20 x=1 y=1 x=1 y=1
Below is another example where the comma (, ) operator is overloaded in the class named Coords3D.
- The class has 3 internal hidden variables x, y, z.
- Get() method which is the access method to get the values of x, y, z.
- The operator function operator, (), which overloads the operator ‘, ‘.
Below is the program for the same:
x = 2 y = 4 z = 6 x = 10 y = 15 z = 20
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Operator Overloading '<<' and '>>' operator in a linked list class
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- How can we use Comma operator in place of curly braces?
- Rules for operator overloading
- Operator Overloading in C++
- Overloading Subscript or array index operator  in C++
- C++ | Operator Overloading | Question 10
- Overloading New and Delete operator in c++
- C++ Program to concatenate two strings using Operator Overloading
- Operator overloading in C++ to print contents of vector, map, pair, ..
- Types of Operator Overloading in C++
- Increment (++) and Decrement (--) operator overloading in C++
- C++ program to compare two Strings using Operator Overloading
- Count number of Unique Triangles using Operator overloading
- Comma in C and C++
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.