I have a class named "Yen" and I need to overload the prefix and postfix -- operators for that class.
Any ideas?
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 }
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
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!"
Last edited by Cat; 06-19-2003 at 05:50 PM.
postfix should return an object not a reference.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
>>> 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.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Why?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.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.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.