Question about over loading



 DEVELOP > c-Plus-Plus > Question about over loading

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Udaya Bhaskar"
Date: 07 Nov 2004 06:09:47 AM
Object: 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++.moderated. First time posters: Do this! ]
.

User: "Mike Wahler"

Title: Re: Question about over loading 07 Nov 2004 06:16:52 PM
"Udaya Bhaskar" <ubyalamanchi@gmail.com> wrote in message
news:81083505.0411060845.65b6cbaf@posting.google.com...

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?

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
.
User: "Michiel Salters"

Title: Re: Question about over loading 08 Nov 2004 08:45:30 AM
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message news:<UVyjd.8307$O11.5445@newsread3.news.pas.earthlink.net>...

"Udaya Bhaskar" <ubyalamanchi@gmail.com> wrote in message
news:81083505.0411060845.65b6cbaf@posting.google.com...

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?


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.

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
.
User: "Allan W"

Title: Re: Question about over loading 08 Nov 2004 06:41:48 PM

"Mike Wahler" <mkwahler@mkwahler.net> wrote

'int' was
probably chosen because it's likely to be the most
efficient type to pass as an argument.

Michiel.Salters@logicacmg.com (Michiel Salters) wrote

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.

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.
.



User: "Victor Bazarov"

Title: Re: Question about over loading 07 Nov 2004 11:33:33 AM
"Udaya Bhaskar" <ubyalamanchi@gmail.com> wrote...

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?

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

User: "chris"

Title: Re: Question about over loading 09 Nov 2004 06:36:06 AM
Udaya Bhaskar wrote:

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?

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++.moderated. First time posters: Do this! ]
.

User: "Rolf Magnus"

Title: Re: Question about over loading 07 Nov 2004 05:58:09 PM
Udaya Bhaskar wrote:

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?

What advantage would that have?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER