473,378 Members | 1,351 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,378 software developers and data experts.

i++ and ++i when the ++ operator is overriden

Hi,

In this board's FAQ, the operator override section says the i++ and ++i
has some difference, the i++ will make a copy of this pointer.
I can't know the reason, why?

Thanks in advance.
ABAI

Oct 24 '06 #1
3 1083
Binary wrote:
Hi,

In this board's FAQ, the operator override section says the i++ and ++i
has some difference, the i++ will make a copy of this pointer.
I can't know the reason, why?
i++ is supposed to return the value of i *before* incrementing. So, if you
implement postincrement for a class, the typical implementation looks
somewhat like this:

class SomeIterator {

// ...

SomeIterator operator++ ( int ) {
// save the old value
SomeIterator return_value ( *this );
// increment
++ *this;
// return the old value
return( return_value );
}

};
Best

Kai-Uwe Bux
Oct 24 '06 #2
Perfect! Thank you!

ABAI
Kai-Uwe Bux wrote:
Binary wrote:
Hi,

In this board's FAQ, the operator override section says the i++ and ++i
has some difference, the i++ will make a copy of this pointer.
I can't know the reason, why?

i++ is supposed to return the value of i *before* incrementing. So, if you
implement postincrement for a class, the typical implementation looks
somewhat like this:

class SomeIterator {

// ...

SomeIterator operator++ ( int ) {
// save the old value
SomeIterator return_value ( *this );
// increment
++ *this;
// return the old value
return( return_value );
}

};
Best

Kai-Uwe Bux
Oct 24 '06 #3
Binary wrote:
Hi,

In this board's FAQ, the operator override section says the i++ and ++i
has some difference, the i++ will make a copy of this pointer.
/should/ make a copy. Each built-in operator++(int) makes a copy, and
your
versions should behave the same. The same applies to operator==; your
versions should return a bool. Else, people who use it would be
surprised.

I.e. it's not a technical requirement but a usability reason.

HTH,
Michiel Salters

Oct 24 '06 #4

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

Similar topics

45
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
1
by: Prithvis | last post by:
My class hierarchy is something like C1 -> C2 -> C3 and in C3 I override a method from C2 which in turn has overriden this method from C1, is there a way to call C1's version of the method? This...
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
2
by: Sput | last post by:
I have overriden DataGridTextBoxColumn but it has lost it's overloads (so I cannot set datatype)? Is there a way to set datatypes to column (and how come that it does not get datatype property from...
5
by: Richard Lewis Haggard | last post by:
What is the syntax to over ride the array index operator ''? I want to derive a class from CollectionBase and have it encapsulate an array of another class I'm working with. Among other things,...
0
by: NetronProject | last post by:
My control inherites from ScrollableControl and overrides the BackColor property. The DescriptionFromAttribute inherits from the DescriptionAttribute to return localized information. The same for...
0
by: cheloman12 | last post by:
Hi, I’m implementing SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium in asp.net 2.0 to increment performance avoiding sending viewstate of pages to browser. The code I...
1
by: cheloman12 | last post by:
Hi, I posted this problem some days ago. I repeat it because it was no answered. If description is confused, please make me know and i'll try to explain the case better. Thanks a lot, Marcelo...
1
by: =?Utf-8?B?U3RldmUgUmFuZGFsbA==?= | last post by:
I have a form with a number of panel controls on it. I have overriden the paint event (of the panel controls) to provide custom painting. What I have done is to use the Paint event to paint the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.