Please consider this bit of code:
#include <iostream>
int main()
{
struct A
{
int x;
int y;
int z;
int foo()
{
std::cout << "enter foo: " << this->x << "," << this->y << "," << this->z << std::endl;
return 5;
}
int moo()
{
std::cout << "enter moo: " << this->x << "," << this->y << "," << this->z << std::endl;
this->x = 1;
this->z = 10;
return 2;
}
};
A b { b.foo(), b.z = b.moo(), 3};
std::cout << "final: " << b.x << "," << b.y << "," << b.z << std::endl;
return 0;
}
The result in my VS2017 (x64 release):
enter foo: 0,0,0
enter moo: 5,0,0
final: 1,2,3
Result from ideone.com (gcc 6.3) https://ideone.com/OGqvjW ):
enter foo: 0,0,3
enter moo: 5,0,3
final: 1,2,2
One compiler sets the member z
to 3 immediately, before everything, then overwrites it when calling methods and assignments, and the other at the very end, after all.
Q. What will be the explanation for this behavior?
Thank.