the difference between post-increment vs pre-increment

Hell all ,
Can anybody explain this in details, please if you can draw a memory model
of how c++ compiler does the increment , and why a is 6 and b is still 5 in this example ?


int a = 5;
int b = a++;
cout << b << endl;
cout << a << endl;



Thanks.
Last edited on
Presumably you meant:
1
2
    int a = 5;
    int b = a++;


I don't know what drawing the memory out will buy you. a and b are distinct variables/objects (which may not be occupying any memory.) The expression a++ evaluates to the value of a pre-increment and has the side effect of incrementing a by 1.

Thus, b is set to the value of a pre-increment and a is incremented by 1.
Topic archived. No new replies allowed.