I'm learning C++ and I have this problem:
#include <iostream>
using namespace std;
class test
{
public:
test(){};
test(int i):var{i}{};
test& operator++(){++var; return this;}
test operator++(int dummy){test tmp =*this;var++;return tmp;}
friend ostream& operator<<(ostream&, const test&);
private:
int var;
};
ostream& operator<<(ostream& o, const test& obj)
{
o<<obj.var;
return o;
}
int main()
{
test obj{2};
cout << obj << endl;
obj++;
cout << obj << endl;
cout << obj <<' '<< ++obj<<endl;
return 0;
}
the output i expected was: 2 3 3 4
instead i have: 2 3 4 4
if i replace the last increment ++obj with obj++ the situation is even more weird: 2 3 4 3
it's like the stream is read in the opposite way, can you help me?
Aucun commentaire:
Enregistrer un commentaire