473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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! ]
Jul 22 '05 #1
6 1695
"Udaya Bhaskar" <ub**********@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
Jul 22 '05 #2
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! ]
Jul 22 '05 #3

"Udaya Bhaskar" <ub**********@gmail.com> wrote in message
news:81**************************@posting.google.c om...
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
Jul 22 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<UV*****************@newsread3.news.pas.earth link.net>...
"Udaya Bhaskar" <ub**********@gmail.com> wrote in message
news:81**************************@posting.google.c om...
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
Jul 22 '05 #5
> "Mike Wahler" <mk******@mkwahler.net> wrote
'int' was
probably chosen because it's likely to be the most
efficient type to pass as an argument.

Mi*************@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.
Jul 22 '05 #6
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! ]
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Frank Wood | last post by:
Hi, I am cross posting this because I'm not really sure which area (HTML/DHTML, Javascript, CSS, etc.) I need to post this question in. I'm also fairly new to all this - please bare with me. I...
0
by: AMDIRT | last post by:
I have a few questions about IssueVision (from WindowsForms) concerning its scalability and performance. Rather, if I were to implement techniques described here into another application, how...
3
by: Rolan | last post by:
To those that have been around Access long enough, I'm sure that insight can be shared as to what aspects of computer processing speed improves its performance. Perhaps in some respects even...
8
by: glenn | last post by:
If I setup a connection and the appropriate items in C# to link a queried dataset that comes back with 100,000 results to a grid component on a windows form, does that mean that my program just...
4
by: BM | last post by:
Hello, Ok, Im new to using ASP.NET and IIS so im not sure how or where to post this question but here goes: I am running IIS 5.1 on WinXP. I need to process http POST messages sent from...
13
by: Chris | last post by:
Can someone tell me the OOP way to code classes for small tables of data? For example, i'm loading several tables that will be stored in the current.cache of an asp.net app. One table is credit...
11
by: Agnes | last post by:
In my form load() Dim daSeaInvoice As New SqlDataAdapter("select * from InvoiceHeader ;select * from invoicedetail, conSea) dim myDataSet as new dataset() daSeaInvoice.fill(mydataset). As I...
2
by: Bob | last post by:
I have noticed that as one adds related tables to a bindingsource and dataset(vs2005 Vb.Net Sql Server 2005), the TODO and following code that fills the datasets on form load are written to the...
18
by: Narshe | last post by:
I've been struggling with this for a while. I have a business entity Employee that has a Company entity attached to it. Ex: public class Compay{ // blah } public class Employee
14
by: Rob Wilkerson | last post by:
Hey all - Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information. What I was surprised to find was that the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.