Thread: overloading postfix/prefix operators for a class

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    overloading postfix/prefix operators for a class

    I have a class named "Yen" and I need to overload the prefix and postfix -- operators for that class.

    Any ideas?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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.:

    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)
    };
    1) Friend or non-friend is irrelevant; the difference is unary form or binary form.
    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.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >>> 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.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Stoned_Coder
    postfix should return an object not a reference.
    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.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    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.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about class design and overloading
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2007, 02:02 PM
  2. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Compile errors with overloading >> operators
    By GMHummerH1 in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2004, 07:13 PM
  5. Overloading Operators
    By Raven Arkadon in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2001, 10:24 AM