#include <iostream>
using namespace std;
class A {
public:
A() { a.a = a.b = 1; }
struct { int a,b; } a;
int b(void);
};
int A::b(void) {
int x=a.a;
a.a=a.b;
a.b=x;
return x;
};
int main(void) {
A a;
a.a.a = 0;
a.b();
cout << a.b() << a.a.b << endl;
return 0;
}
I am currently practicing for a C++ certification and I came across this question. I found it strange that after the cout, if I print only the a.a.b value, it prints 1, but the code as it is displays 10, so the value of a.a.b is considered 0. From the research I've done online it seems that this is undefined behaviour as there is no guarantee that in the cout call the compiler will first evaluate the a.b() call and then evaluate the a.a.b value. Is there a definite order in which the compiler should evaluate the expressions in this cout?