Problem with overloading << and ++ operators

Hello i have 2 similar small programs using the overloading of << and ++ operator but i don't understand why they give me different results. It would be really helpful if someone can enlighten me on this.

All i've done is change the order of variables after the cout statment.

cout << i++ << i.v; to cout << i.v << i++

Code 1 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator++(int x) {
            v+=2;
            return *this;
        }
        
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }
    
    int main() {
        Int i = 0;
        cout << i++ << i.v; 
        return 0;
    }


result is : 20

Code 2 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator++(int x) {
            v+=2;
            return *this;
        }
        
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }
    
    int main() {
        Int i = 0;
        cout << i.v << i++; 
        return 0;
    }


result is :22

Edit : result for the second code was 22
Last edited on
closed account (EwCjE3v7)
Which is evuluated first? the << or ++?

It is undefined, here are some references
http://herbsutter.com/gotw/_102/
http://en.cppreference.com/w/cpp/language/eval_order

But I can`t seem to fix your code sorry.
Last edited on
Thank you for the answer. Now, i understand that they should not be put together and we have an undefined behaviour but i got this on a test and i needed to give a precise answer. So is there any way to predict the result ?
Last edited on
closed account (EwCjE3v7)
Yes there is.

Lets look at your code except with the << and ++ on different lines.
(Read comments)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator++(int x) {
            v+=2; // as you see here when you use the suffix(after i) ++ it adds 2 to the the value of v
            return *this;
        }

    };

    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }

    int main() {
        Int i = 0;
        cout << i.v << " "; // prints 0
        i++; // adds 2
        cout << i.v << endl; // prints 2
        return 0;
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator++(int x) {
            v+=1; // now I have changed this to add one so it will add one
            return *this;
        }

    };

    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }

    int main() {
        Int i = 0;
        cout << i.v << " ";
        i++; // add 1 not 2 as the previous code
        cout << i.v << endl;
        return 0;
    }


As you can see your code adds 2 and than I have changed it to add 1 in the second one.

You can predict it if you look at line 8(v+=2;), what ever number is in the place of 2 is the number you will be adding onto the original number
Last edited on
i understand what you explained here but i still don't get why:

With this code cout << i++ << i.v ;
i obtain a result of : 20
Normally If we evaluate i++ that means i.v should be equal 2 right ?

And with this line : cout << i.v << i++;
i obtain a result of : 22
That means i++ was evaluated before i.v ??
Last edited on
closed account (EwCjE3v7)
I to do not understand why :D. Someone else has to explain this. Sorry
No problem thank you for your answers though =) . Should i post the question in another section of the forum or this is the good place?
That means i++ was evaluated before i.v ??

You were already pointed to http://en.cppreference.com/w/cpp/language/eval_order

The result is not predictable: there is no rule that says that i++ should be called before or after the read from i.v. It can go either way.

I'll demonstrate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
struct Int {
    int v;
    Int(int a) : v(a) {}
    Int& operator++(int) { v+=2; return *this; }
};
std::ostream& operator<<(std::ostream& o, const Int& a) { return o << a.v; }
int main()
{
    Int i = 0;
    std::cout << i++ << i.v  << '\n';
    i = 0;
    std::cout << i.v << i++ << '\n';
}


clang/linux:
$ ./test
22
02

gcc/linux:
$ ./test
20
22

gcc/ibm:
$ ./test
22
02

xlc/ibm:
$ ./test
22
02

oracle/sparc:
$ ./test
22
22
Ow i see , thank you very much for the answer =).
It's weird that i got this question on a test with no other possible answer than 22 20 02.
That just means whoever wrote the test doesn't know the subject. All too common with C++ (and C, it has the same expression evaluation rules).
Topic archived. No new replies allowed.