struct BLA
{
int size_;
int size()const{ return size_; }
}
int x;
BLA b[ 2 ];
BLA * p = &b[ 0 ];
b[ 0 ].size_ = 4;
b[ 1 ].size_ = 6;
When I compile this line:
x = p->size_ + (p++)->size_;
I receive the expected result. But, when I compile this line (without the previous one):
x = p->size() + (p++)->size();
Then I get different result. The ‘p’ is not incremented at the same time as in the previous line. Can someone explain this, please? Tried on VS 2008 and VS 2010.
It’s undefined behaviour to seperately read and modify a variable without an intervening sequence point. You’ve seen a good example of the consequences of that.