473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference in overloading Post increment and Pre increment operator in C++

1 New Member
Hi,
Can anyone explain me the major diference in overloading preincrement and post increment operators?
Regards,
Smita
Sep 3 '07 #1
2 6343
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,
Can anyone explain me the major diference in overloading preincrement and post increment operators?
Regards,
Smita
The major difference between the post and pre increment is how u define the function.
Expand|Select|Wrap|Line Numbers
  1. <class name> &operator++(); // pre-increment
  2. <class name> operator++(int); // post-increment
  3.  
Thanks
Raghuram
Sep 3 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
There's a little more to it than that.

Expand|Select|Wrap|Line Numbers
  1. if (++i == 10)
  2. {
  3.  
  4. }
  5.  
In the above code, if i is 10, the test is made on 11. The prefix increment defines the variable is increased before it is used.

When you overload the prefix increment, you return the object after it is incremented.

Expand|Select|Wrap|Line Numbers
  1. if (i++== 10)
  2. {
  3.  
  4. }
  5.  
In the above code, if i is 10, the test is made on 10. Then i is incrmented to 11. The postifx increment defines the variable is used and then incremented.

When you ovarload the postfix increment, you save a copy of the object, increment the original object and then return the saved copy that has the original value.
Sep 3 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1755
by: Grant Austin | last post by:
Hi all, I'm trying to overload the '==' operator for a class, Page, that I wrote so that I can use the STL List 'find' algorithm. I'm having difficulties getting a definition that g++ likes and...
2
1790
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
2
1913
by: dover | last post by:
Could somebody give hints about writing overloading of post-increment and pre-increment operators? Thanks!
22
3587
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
1593
by: vinnie | last post by:
what's the difference between " i++ " and " ++i " ? Thanks
13
9801
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
5
2846
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using...
4
1934
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
3
3792
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where...
0
7198
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
7072
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
6979
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
7449
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...
1
4998
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
3160
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
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
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 ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.