Look at this simple class:
class A {
int *val;
public:
A() { val = new int; *val = 0; }
int get() { return ++(*val); }
};
Why when I run this code it prints 21
:
int main() {
A a, b = a;
cout << a.get() << b.get();
return 0;
}
But if I run it like this it prints 12
which is what I was expecting:
int main() {
A a, b = a;
cout << a.get();
cout << b.get();
return 0;
}
What am I missing here? Operator precedence? FYI, this is a C++ test problem, not a production code.
EDIT:
Does it means that when I have cout << (Expr1) << (Expr2)
then Expr1
and Expr2
are evaluated before the output of Expr1
is printed?