#include <iostream>
using namespace std;
struct X
{
int i_Val;
X(int iVal)
:i_Val(iVal)
{
}
X& operator++()
{
cout << "X::operator++()" << endl;
++i_Val;
return *this;
}
operator int() const
{
return i_Val;
}
};
const X operator+(const X& lhs, const X& rhs)
{
cout << "operator+(const X&, const X&)" << endl;
return lhs.i_Val + rhs.i_Val;
}
int main()
{
X x = 5;
X y = (++x) + (++x) + (++x);
cout << y << endl;
}
compile and run. It produces the output:
X::operator++()
X::operator++()
X::operator++()
operator+(const X&, const X&)
operator+(const X&, const X&)
24
But I expected this:
X::operator++()
X::operator++()
operator+(const X&, const X&)
X::operator++()
operator+(const X&, const X&)
22
Who's at blame? Me or the compiler?