Question about over loading

U

Udaya Bhaskar

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

Victor Bazarov

Udaya Bhaskar said:
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
 
R

Rolf Magnus

Udaya said:
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?
 
M

Mike Wahler

Udaya Bhaskar said:
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
 
M

Michiel Salters

Mike Wahler said:
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
 
A

Allan W

(e-mail address removed) (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.
 
C

chris

Udaya said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top