I have a class named "Yen" and I need to overload the prefix and postfix -- operators for that class.
Any ideas?
Printable View
I have a class named "Yen" and I need to overload the prefix and postfix -- operators for that class.
Any ideas?
The prefix function needs to be a friend function (or otherwise outside the class), and the postfix needs to be a member function.
Code:class A
{
public:
friend A operator++(A&); // Prefix
A operator++(A&); // Postfix
}
Actually, the proper way is to make the postfix take a dummy int argument (make it have the form of a binary operator). Either can be friend or member. The dummy argument is unused and irrelevant.
E.g.:
1) Friend or non-friend is irrelevant; the difference is unary form or binary form.Code:Class C{
C& operator++(); // prefix (form is unary member)
C& operator++(int); // postfix (form is binary member)
};
Class C2{
friend C2& operator++(C2& obj); // prefix (form is unary friend)
friend C2& operator++(C2& obj, int); //postfix (form is binary friend)
};
2) If you use the postfix operator, the final parameter is USELESS; it's bad to name that parameter; you can't and shouldn't use it for anything. Its sole purpose is to make the function declaration/definition be parsed as a binary function.
I prefer leaving this as a nameless int; it's much more obviously a dummy var. The type must be int.
Zach's prefix example is OK (typical friend unary form) but his postfix would fail; it would be OK if the parameter was of type int (and thus typical member binary form).
Note that both prefix and postfix operators actually are both UNARY operators. In all expressions, they act as unary operators. When overriding, the postfix is given the FORM of a binary operator to make it distinct from the prefix, but it IS a unary operator when it is used.
Just think of postfix as taking a dummy "int" parameter for no reason other that to say "Hey, I'm postfix!"
postfix should return an object not a reference.
>>> Zach's prefix example is OK (typical friend unary form) but his postfix would fail;
Indeed it would. I was a bit hasty posting that, sorry. Should've been:
A operator++(int); as Cat said.
Sorry 'bout that.
Why?Quote:
Originally posted by Stoned_Coder
postfix should return an object not a reference.
Given a possible command:
++(++e)
If e returned an reference, it would increment itself twice. If it returned an object, It would increment itself once, and the object created would be incremented again. (The original object would be incremented only once.)
Of course, these examples rarely come up, but it's still good practice to provide for them.
Generally with postfix incrementing, you return the value as it was prior to being incremented. This means that you have to store that prior value as a local variable with function scope, and, after returning the reference to it, it goes out of scope.Quote:
Originally posted by ygfperson
Why?
Given a possible command:
++(++e)
If e returned an reference, it would increment itself twice. If it returned an object, It would increment itself once, and the object created would be incremented again. (The original object would be incremented only once.)
Of course, these examples rarely come up, but it's still good practice to provide for them.