Reputation: 1801
Below is the code.
#include<iostream>
using namespace std;
class x {
int a;
public :
x(int t=2):a(t) {}
void print (){
cout <<"value is "<<a;
}
x& operator,(x&a){
return *this;
}
};
int main(){
x a(1),b(2),c(3),d(4);
x t=(a,b,c,d);
t.print();
return 0;
}
output value is 1
please explain why the value is not 4 in this line x t=(a,b,c,d);
Upvotes: 1
Views: 136
Reputation: 9071
x t = (a,b,c,d);
No matter what order this expression is evaluated in, the left-most operand will always be returned because this
in your x& operator , (x &instance)
refers to the left operand while instance
refers to the right.
It is thus returning a
and you are getting a printed value of 1
.
If you didn't overload the comma operator, you may get 4
because an expression like (a, b, c)
will return the right-most operand.
Upvotes: 2