Overloading the Comma Operator
Last Updated :
06 Oct, 2020
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.
Examples:
Input: x = (y = 5, y + 2)
Output: x = 7, y = 5
Explanation:
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:
Syntax:
return_type class_name::operator op(argument_list)
{
// body
}
where,
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.
CPP
// C++ program to illustrate the
// overloading the comma operator
#include <iostream>
using namespace std;
// Comma class
class comma {
int x, y;
public:
// Default Constructor
comma() {}
// Parameterized Constructor
comma(int X, int Y)
{
x = X;
y = Y;
}
// Function to display the value
void display()
{
cout << "x=" << x << " ";
cout << "y=" << y << " ";
}
comma operator+(comma ob2);
comma operator, (comma ob2);
};
// Function to overload the + operator
comma comma::operator+(comma ob2)
{
comma temp;
// Update the value temp
temp.x = ob2.x + x;
temp.y = ob2.y + y;
// Return the value temp
return temp;
}
// Function to overload the, operator
comma comma::operator, (comma ob2)
{
comma temp;
// Update the value temp
temp.x = ob2.x;
temp.y = ob2.y;
// Print the value
cout << "x=" << ob2.x << " "
<< "y=" << ob2.y << endl;
// Return the value temp
return temp;
}
// Driver code
int main()
{
// Initialize objects
comma ob1(10, 20), ob2(5, 30), ob3(1, 1);
ob1.display();
ob2.display();
ob3.display();
cout << endl;
ob1 = (ob2 + ob2, ob1, ob3);
// Displays the value of
// ob3 (Rightmost expression)
ob1.display();
return 0;
}
Output:
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:
C++
// C++ program to illustrate the
// overloading for comma operator
#include <iostream>
using namespace std;
// The class that defines the
// coordinates of a point in space
class Coords3D {
private:
double x, y, z;
public:
// Default Constructor
Coords3D() { x = y = z = 0; }
// Parameterized Constructor
Coords3D(double x, double y,
double z)
{
this->x = x;
this->y = y;
this->z = z;
}
// Function for updating the value
// ofx, y, and z
void Get(double& x, double& y,
double& z)
{
x = this->x;
y = this->y;
z = this->z;
}
// Function to overloaded the
// operator, (comma)
Coords3D operator, (Coords3D obj)
{
Coords3D tmp;
// Update the value of temp
tmp.x = obj.x;
tmp.y = obj.y;
tmp.z = obj.z;
// Return the value of temp
return tmp;
}
};
// Driver Code
int main()
{
double x, y, z;
// Instances of class Coords3D
Coords3D c1(1, 3, 5);
Coords3D c2(2, 4, 6);
Coords3D c3;
// Invoke the operator function
// c3.operator, (c2) into c3
// is saved c2
c3 = (c1, c2);
// Get the value of x, y, z
c3.Get(x, y, z);
// Print x, y, and z
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
// Create another instance
Coords3D c4(10, 15, 20);
// c3 <= c4
c3 = (c2, c1, c4);
// Checking
// x = 10, y = 15, z = 20
c3.Get(x, y, z);
cout << endl;
// Print x, y, and z
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
return 0;
}
Output:
x = 2
y = 4
z = 6
x = 10
y = 15
z = 20
Similar Reads
Types of Operator Overloading in C++ C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change some specific operators' functions to do different tasks.Syntax: Return_Type classname :: op
4 min read
Rules for operator overloading 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) O
3 min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Logical (&&, ||, !) Operator Overloading Prerequisites: OperatorsOperator Overloading Logical operators are used for combining two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. In C++, there are 3 logical operators: Logi
3 min read
Typecast Operator Overloading in C++ In C++, the typecast operator can be overloaded to customize the behavior of casting operators to define how user-defined data types can be converted into other types. This enables developers to define how instances of a class are converted to other types, providing more control over implicit type c
6 min read
Input/Output Operators Overloading in C++ Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(>>/<<) Overloading in C++ We c
2 min read