Overloading ++ for both pre and post increment

The postfix version of the increment operator takes a dummy int parameter in order to disambiguate:

// prefix
CSample& operator++()
{
  // implement increment logic on this instance, return reference to it.
  return *this;
}

// postfix
CSample operator++(int)
{
  CSample tmp(*this);
  operator++(); // prefix-increment this instance
  return tmp;   // return value before increment
}

Leave a Comment