0

I was overloaded the two operators: operator<< and operator++

operator<<

ArithmeticSequence & ArithmeticSequence::operator++()
{
    this->lastElement += goToNext;
    this->sum += lastElement;

    return *this;
}

operator++

ostream & operator<<(ostream & out, const ArithmeticSequence as)
{
    out << as.sum;
    return out;
}

now the main is:

ArithmeticSequence AS(1.3, 2.5);
cout << "++AS sum: " << ++AS << " ++AS sum: " << ++AS << endl;

output:

++AS sum: 20.2 ++AS sum: 11.4 

the correct output need to be:

++AS sum: 11.4 ++AS sum: 20.2

why the output is revers?

zcbd
  • 75
  • 9

1 Answers1

0

There's no sequence point in between your two ++A5 so they can be evaluated in any order.

This will work as expected:

 cout << "++AS sum: " << ++AS;
 cout << " ++AS sum: " << ++AS << endl;
Paul Evans
  • 26,111
  • 3
  • 30
  • 50