Postfix ++ Overloading

Hi,

I understand that in C++, the prefix ++ (++i) is default for the unary ++ operator. However, right now I am trying to implement i++ and I am not sure how to exactly distinguish that from prefix ++. I read that I need to give it a dummy variable int to distinguish. However, how does the compiler know I want postfix ++? Are we making it a binary operator?

1
2
3
template<typename T>
T Vec::operator++(int);    //How does the compiler know I am trying to make postfix ++?
 //Are we actually making ++ a binary operator?  
Last edited on
You have it right. Only in the cases of the "++" and "--" operators, returning by value (not a reference) and taking an int is the "signal" to the compiler that this is a postfix version. It works on no other operators, and you ignore the integer parameter. It's a hacked syntax, and no one really likes it, but that's what they came up with at the time. There is some convoluted logic to it, but not that seems justifying to the form you see - it is that alternatives were worse for various reasons (there's a book with that history in it, I read year ago, and happily forgotten).

It is not really a binary operator.

Haha I see. Thanks!

Rico


Niccolo (439)
You have it right. Only in the cases of the "++" and "--" operators, returning by value (not a reference) and taking an int is the "signal" to the compiler that this is a postfix version. It works on no other operators, and you ignore the integer parameter. It's a hacked syntax, and no one really likes it, but that's what they came up with at the time. There is some convoluted logic to it, but not that seems justifying to the form you see - it is that alternatives were worse for various reasons (there's a book with that history in it, I read year ago, and happily forgotten).

It is not really a binary operator.
Believe it or not, the place I have bookmarked for reference is Wikipedia’s page:

    https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

It is very concise and easy to read.
Topic archived. No new replies allowed.