Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

could not able to understand how comma operator works

Tags:

c++

In this below program I have overloaded commaoperator. But, why comma operator is not taking considering the first element/object.

class Point {
  int x, y;
public:
  Point() {}
  Point(int px, int py) 
  {x = px;y = py;}
  void show() {
    cout << x << " ";
    cout << y << "\n";
  }
  Point operator+(Point op2);
  Point operator,(Point op2);
};

// overload comma for Point
Point Point::operator,(Point op2)
{
  Point temp;
  temp.x = op2.x;
  temp.y = op2.y;
  cout << op2.x << " " << op2.y << "\n";
  return temp;
}

// Overload + for Point
Point Point::operator+(Point op2)
{
  Point temp;
  temp.x = op2.x + x;
  temp.y = op2.y + y;
  return temp;
}

int main()
{
  Point ob1(10, 20), ob2( 5, 30), ob3(1, 1);
  Point ob4;
  ob1 = (ob1, ob2+ob2, ob3);//Why control is not reaching comma operator for ob1?
  ob1 = (ob3, ob2+ob2, ob1);//Why control is not reaching comma operator for ob3?
  ob4 = (ob3+ob2, ob1+ob3);//Why control is not reaching comma operator for ob3+ob2?
  system("pause");
  return 0;
}

I have tried to understand , operator also but could not able to find the solution.

  ob1 = (ob1, ob2+ob2, ob3);//Why control is not reaching comma operator for ob1?
  ob1 = (ob3, ob2+ob2, ob1);//Why control is not reaching comma operator for ob3?
  ob4 = (ob3+ob2, ob1+ob3);//Why control is not reaching comma operator for ob3+ob2?

Any help appreciated.

like image 702
Rasmi Ranjan Nayak Avatar asked Sep 18 '13 12:09

Rasmi Ranjan Nayak


People also ask

How does comma operator work?

The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.

How does comma operator work in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

What is comma operator in C++ with example?

In C and C++, comma (, ) can be used in two contexts: 1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).

What is comma operator in Python?

Python does not have a "comma operator" as in C. Instead, the comma indicates that a tuple should be constructed. The right-hand side of a, b = a + b, a. is a tuple with th two items a + b and a .


2 Answers

Why control is not reaching comma operator for ob1?

I guess you're asking, why does this line only output two points: 10 60 for ob2+ob2, and 1 1 for ob3. This is because you only invoke the comma operator twice; each time, it outputs its right-hand argument and ignores its left-hand argument.

The line of code is equivalent to

ob1.operator,(ob2+ob2).operator,(ob3);

making it clear that it's only called twice. ob1 is evaluated, but the operator doesn't do anything with it.

like image 160
Mike Seymour Avatar answered Sep 30 '22 10:09

Mike Seymour


It does reach it. But since you're only printing out the values of the argument of operator, and never of this, you don't get to see it printed out.

like image 42
Nikos C. Avatar answered Sep 30 '22 12:09

Nikos C.