473,467 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

operator ++ overloading

hi:

in the following code:

class plus{

int data_item;

public:

plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}

plus operator++(int val) {

data_item += val;
return *this;

}
};

....

plus a(10), b(20);

++a;
b++;

I have the following questions:

1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?

2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.

many thanks,

bo

Jul 22 '05 #1
2 1787
Bo Sun wrote:
hi:

in the following code:

class plus{

int data_item;

public:

plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}

plus operator++(int val) {

data_item += val;
return *this;

}
};

...

plus a(10), b(20);

++a;
b++;

I have the following questions:

1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?
That's just the way the language is defined. Passing an int parameter
is a real hack, useful only for differentiating between pre- and post-
operators. The int isn't used for anything.
2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.


Why, that's so crazy it just might work... Try this:

b.operator ++ ( 10 );

Here's some sample code to lead the way; by the way, just for the
record, I hope you are doing this only as a novelty. I mean, this is a
really ugly contortion of C++.

/* I'm going to Hell for this.
*/
#include <iostream>

struct C
{
C operator ++ ( int val )
{
C old_value = *this;

std::cout << val << std::endl;

// ... alter *this ..

return old_value;
}
};
int main( )
{
C c;

c.operator ++ ( 10 );
}
Jul 22 '05 #2

"Bo Sun" <b0*****@cs.tamu.edu> wrote in message
news:Pi*******************************@unix.cs.tam u.edu...
hi:

in the following code:

class plus{

int data_item;

public:

plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}

plus operator++(int val) {

data_item += val;
return *this;

}
};

...

plus a(10), b(20);

++a;
b++;

I have the following questions:

1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?
This is just by convention. When C++ was being created, they had to come up
with some way to differentiate between the prefix and postfix forms of
operator++ (and operator-- too for that matter), so this is the way they did
it. The convention is what you've described in question 1: the prefix form
takes no parameters and the postfix form takes one int parameter.

So, for the postfix form, where does the parameter come from and what is its
value? The answer is that the compiler supplies it (unbeknownst to you) and
it has a value of 0. So, when the compiler sees something like a++, it
calls operator++ and passes an int argument of value 0, and so the postfix
form gets called since it takes an int parameter. For the expression ++a,
no argument would be passed behind-the-scenes by the compiler, so the prefix
form would get called since it takes no parameters.
2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.
The reason this does not work is because the postfix form of operator++
takes one parameter - the hidden parameter I've mentioned above. In the
b++(10) call you show, there are actually two parameters - the hidden one
AND the value of 10 you're trying to pass. There is no version of
operator++ that takes two parameters, so this line does not compile. You
*could* do the following (but I'll explain below why you shouldn't):

b.operator++(10); // Call the operator using function call syntax

This would give you the result you want, but...

operator++ (and operator--) should only be used to increment (decrement) by
1. This is because that is the established convention in C++. To do
otherwise would make your class very confusing to anybody trying to use it.
Everybody expects an increment (decrement) by 1, so to do otherwise would
really throw people off.

To do what you desire, I suggest overloading operator+=.

many thanks,

bo

Jul 22 '05 #3

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
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:
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.