Is the behavior of return x++; defined?

patros picture patros · Mar 4, 2010 · Viewed 17.4k times · Source

If I have for example a class with instance method and variables

class Foo
{

   ...

   int x;
   int bar() { return x++; }
 };

Is the behavior of returning a post-incremented variable defined?

Answer

Peter Alexander picture Peter Alexander · Mar 4, 2010

Yes, it's equivalent to:

int bar()
{
  int temp = x;
  ++x;
  return temp;
}