473,624 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ 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 1801
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.tam u.edu> wrote in message
news:Pi******** *************** ********@unix.c s.tamu.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
2596
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
34
6437
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 operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
16
3081
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2057
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 overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
67
8625
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 in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
3
2287
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
3619
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: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
3
3272
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, obj2 ;
9
3492
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 allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
8
2967
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, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
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 we have to send another system
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.