Question about over loading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Udaya Bhaskar

    Question about over loading

    To differentiate between postfix and prefix opeators , when
    overloaded, we need to pass a dummy parameter.

    Why should we pass ONLY int to overload postfix ++ / -- operator?

    Why not a FLAOT/ DOUBLE ?

    or a bit field?

    Thanks,
    Uday.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.m oderated. First time posters: Do this! ]
  • Victor Bazarov

    #2
    Re: Question about over loading

    "Udaya Bhaskar" <ubyalamanchi@g mail.com> wrote...[color=blue]
    > To differentiate between postfix and prefix opeators , when
    > overloaded, we need to pass a dummy parameter.
    >
    > Why should we pass ONLY int to overload postfix ++ / -- operator?
    >
    > Why not a FLAOT/ DOUBLE ?
    >
    > or a bit field?[/color]

    Why? Because the Standard says so, that's why. See 13.5.7.

    V


    Comment

    • Rolf Magnus

      #3
      Re: Question about over loading

      Udaya Bhaskar wrote:
      [color=blue]
      > To differentiate between postfix and prefix opeators , when
      > overloaded, we need to pass a dummy parameter.
      >
      > Why should we pass ONLY int to overload postfix ++ / -- operator?
      >
      > Why not a FLAOT/ DOUBLE ?
      >
      > or a bit field?[/color]

      What advantage would that have?


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.m oderated. First time posters: Do this! ]

      Comment

      • Mike Wahler

        #4
        Re: Question about over loading


        "Udaya Bhaskar" <ubyalamanchi@g mail.com> wrote in message
        news:81083505.0 411060845.65b6c baf@posting.goo gle.com...[color=blue]
        > To differentiate between postfix and prefix opeators , when
        > overloaded, we need to pass a dummy parameter.
        >
        > Why should we pass ONLY int to overload postfix ++ / -- operator?
        >
        > Why not a FLAOT/ DOUBLE ?
        >
        > or a bit field?[/color]

        Theoretically, the type doesn't really matter. We simply need
        *something* in order to differentiate it. 'int' was
        probably chosen because it's likely to be the most
        efficient type to pass as an argument. Finally, we
        must use 'int' because the language standard specifically
        mandates it.

        Why does it matter to you?

        -Mike


        Comment

        • Michiel Salters

          #5
          Re: Question about over loading

          "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<UVyjd.830 7$O11.5445@news read3.news.pas. earthlink.net>. ..[color=blue]
          > "Udaya Bhaskar" <ubyalamanchi@g mail.com> wrote in message
          > news:81083505.0 411060845.65b6c baf@posting.goo gle.com...[color=green]
          > > To differentiate between postfix and prefix opeators , when
          > > overloaded, we need to pass a dummy parameter.
          > >
          > > Why should we pass ONLY int to overload postfix ++ / -- operator?
          > >
          > > Why not a FLAOT/ DOUBLE ?
          > >
          > > or a bit field?[/color]
          >
          > Theoretically, the type doesn't really matter. We simply need
          > *something* in order to differentiate it. 'int' was
          > probably chosen because it's likely to be the most
          > efficient type to pass as an argument. Finally, we
          > must use 'int' because the language standard specifically
          > mandates it.[/color]

          In fact, efficiency doesn't matter. No argument is actually passed
          to the function, just as the =0 in a pure virtual funcion is not
          actually an assignment.
          int was probably chosen because the postfix ++ adds 1, and 1 is
          an int. But your last point is basically what matters

          HTH,
          Michiel Salters

          Comment

          • Allan W

            #6
            Re: Question about over loading

            > "Mike Wahler" <mkwahler@mkwah ler.net> wrote[color=blue][color=green]
            > > 'int' was
            > > probably chosen because it's likely to be the most
            > > efficient type to pass as an argument.[/color][/color]

            Michiel.Salters @logicacmg.com (Michiel Salters) wrote[color=blue]
            > In fact, efficiency doesn't matter. No argument is actually passed
            > to the function, just as the =0 in a pure virtual funcion is not
            > actually an assignment.
            > int was probably chosen because the postfix ++ adds 1, and 1 is
            > an int.[/color]

            Sorry, that's not correct.

            13.5.7 Increment and decrement
            The user-defined function called operator++ implements the prefix
            and postfix ++ operator. ...
            If the function is a member function with one parameter (which
            shall be of type int) or a non-member function with two parameters
            (the second of which shall be of type int), it defines the postfix
            increment operator ++ for objects of that type. When the postfix
            increment is called as a result of using the ++ operator, the int
            argument will have value zero.

            There follows an example in which they call a.operator++(0) and
            operator++(b,0) explicitly passing the value of the int.

            // MY example, shorter than the one in the standard
            #include <iostream>
            using namespace std;
            struct Foo {};
            Foo &operator++( Foo &lhs, int rhs)
            { cout << "rhs is " << rhs << endl; return lhs; }
            int main() {
            Foo f;
            f++; // rhs is 0
            operator++(f,27 ); // rhs is 27
            }

            Using the second parameter in operator++ is unusual at best, and
            obfuscated at worst -- but it's legal.

            Comment

            • chris

              #7
              Re: Question about over loading

              Udaya Bhaskar wrote:[color=blue]
              > To differentiate between postfix and prefix opeators , when
              > overloaded, we need to pass a dummy parameter.
              >
              > Why should we pass ONLY int to overload postfix ++ / -- operator?
              >
              > Why not a FLAOT/ DOUBLE ?
              >
              > or a bit field?
              >[/color]
              The standard actually says that you shouldn't attempt to read from /
              write to that parameter. To be honest, that int parameter is an ugly
              hack to give people a way of overloading seperatly the postfix operator
              (originally, you could only overload prefix, and the postfix operator
              simply made a copy of the object and then did the prefix operator).

              Now, just accept it as a slightly bizarre hack, and ignore the parameter
              (it's best all around to simply not give it a name, and write
              operator++(int) {..})

              Chris

              [ See http://www.gotw.ca/resources/clcm.htm for info about ]
              [ comp.lang.c++.m oderated. First time posters: Do this! ]

              Comment

              Working...