2

Let's say i have a class named A, and i don't have the class implementation. Which will be more expensive on run time scale: The postfix or the prefix ++ operation?

Example:

void some_function{
    ...
    A x;

    ++x;

    x++;

    ...
}

I read online that the suffix x++ operator on classes will do 2 constructor calls, but no explanation was given. I can't find a decent explanation for it. why is it we have 2 calls and none on the other?

Itzik984
  • 15,968
  • 28
  • 69
  • 107
  • 1
    http://en.cppreference.com/w/cpp/language/operator_incdec and http://www.parashift.com/c++-faq/increment-pre-post-speed.html – EdChum Jan 14 '15 at 14:11
  • possible duplicate of [What is the difference between i++ and ++i?](http://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i) –  Jan 14 '15 at 14:14
  • @Satus, It's not a duplicate, it's a different question. i do know the suffix and prefix differences. – Itzik984 Jan 14 '15 at 14:16
  • 4
    You cannot know if you don't know the implementation. – rubenvb Jan 14 '15 at 14:17
  • Prefix: increment x, then return me x. Postfix: create temp variable (that is a copy of x), then increment x, then return me that temp variable. But in C++ you can overload both operators the way you want to. So if you don't know the implementation, you don't know the result of calling `++x` or `x++`. –  Jan 14 '15 at 14:18
  • @Satus, The creation of temp is, in fact, another constructor call. am i wrong? this means since A is not a basic type like int or long, we will always call another constructor, hence, more expansive to execute? – Itzik984 Jan 14 '15 at 14:31
  • @Itzik984 Yes, you are right. –  Jan 15 '15 at 13:17

1 Answers1

7

There are two possible answers to this question, because of the fact you don't know the class's implementation (as you say).

Scenario one: The class is reasonable

In this scenario, the semantics of ++ are similar to those of built-in types and standard library iterators: ++x is preincrement, x++ is postincrement.

In such case, ++x returns the value of x after the increment. It's actually expected of ++x to return x by reference, making ++ ++ x legal. A typical implementation would be:

A& A::operator++()
{
  increment_somehow();
  return *this;
}

As you see, no copying (construction) is necessary.

x++, on the other hand, returns the value before the increment. Which means it must store the value somewhere to be able to return it. A typical implementation would be:

A A::operator++(int)
{
  A old(*this); // copy construction
  ++(*this); // re-use preincrement to keep semantics consistent
  return old;  // move construction of return value (possibly elided)
}

As my comments above show, there are two constructor calls involved (even though one of them can be elided).

Scenario two: The class is arbitrary

If the prefix and postfix ++ are not indended to match standard semantics, then naturally all bets are off, as the functions can do literally anything. Without knowing more about them, they cannot be compared.

Community
  • 1
  • 1
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455