global prefix and postfix ++ operator overloading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.aioe.org

    global prefix and postfix ++ operator overloading

    Is it possible to overload increment(++) and decrement(--) postfix and prefix
    operators for primitive datatypes such as int, char, short, etc. in global scope
    (vs as a class member function where this is possibe)?

    If there is a site that lists all operators that can't be overloaded (such as
    the member access dot operator), please post it.

    thanks
  • mlimber

    #2
    Re: global prefix and postfix ++ operator overloading

    On Aug 20, 5:16 pm, "news.aioe. org" <bigmur...@yaho o.comwrote:
    Is it possible to overload increment(++) and decrement(--) postfix and prefix
    operators for primitive datatypes such as int, char, short, etc. in global scope
    (vs as a class member function where this is possibe)?
    >
    If there is a site that lists all operators that can't be overloaded (such as
    the member access dot operator), please post it.
    The only operators that can be overloaded globally are new and delete
    (in all their forms). All other operator overloads must involve a
    class, and even new and delete can be overloaded on a class-by-class
    basis.

    Cheers! --M

    Comment

    • news.aioe.org

      #3
      Re: global prefix and postfix ++ operator overloading

      mlimber wrote:
      On Aug 20, 5:16 pm, "news.aioe. org" <bigmur...@yaho o.comwrote:
      >Is it possible to overload increment(++) and decrement(--) postfix and prefix
      >operators for primitive datatypes such as int, char, short, etc. in global scope
      >(vs as a class member function where this is possibe)?
      >>
      >If there is a site that lists all operators that can't be overloaded (such as
      >the member access dot operator), please post it.
      >
      The only operators that can be overloaded globally are new and delete
      (in all their forms). All other operator overloads must involve a
      class, and even new and delete can be overloaded on a class-by-class
      basis.
      >
      Cheers! --M
      Agreed, "involving" a class doesn't necessarily mean the operator has to be a
      member function.

      struct C
      {
      int i;
      int operator++() { return i += 2; }
      };

      int operator+=(C& c, int x) { return c.i += x; }

      Comment

      • Bo Persson

        #4
        Re: global prefix and postfix ++ operator overloading

        news.aioe.org wrote:
        Is it possible to overload increment(++) and decrement(--) postfix
        and prefix operators for primitive datatypes such as int, char,
        short, etc. in global scope (vs as a class member function where
        this is possibe)?
        You can't write any operators for built-in types, because they are
        already there. Predefined.


        Bo Persson


        Comment

        Working...