Can't understand the outcome! help

Nov 1, 2013 at 6:32pm
I do not understand why my compiler gives me 21 when I run the code below.
From my understanding, it should give me 12. Can anyone explain me why?

Side note)
If it use: cout << a.get();
cout << b.get(); it gives me 12.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    #include <iostream>
    using namespace std;
    
    class A { 
        int *val;
    public:
        A() { val = new int; *val = 0; }
        int get() { return ++(*val); } 
    };
    
    int main() {
        A a,b = a;
        cout << a.get() << b.get();
        return 0;
    }
Nov 1, 2013 at 6:38pm
The order in which a.get() and b.get() are evaluated on line 13 is unspecified by the standard. Either 21 or 12 would be correct output.
Last edited on Nov 1, 2013 at 6:38pm
Topic archived. No new replies allowed.