Link to home
Start Free TrialLog in
Avatar of lzha022
lzha022

asked on

how to implement pre/post increment operator

Hello experts,
Could you please show me how to implement pre/post increment operator to this class.
public class Foo()
{

}

Thanks.
lzha022
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
pepr

... as evilrix pointed out.

    returntype  operator++()
   {
        ... your code for prefix increment here...
        return result;  
   }

    returntype  operator++(int)
   {
        ... your code for postfix increment here...
        return result;  
   }

Notice the int as a dummy argument of the postfix increment version. This is the way how the compiler distinguishes if you want prefix or postfix operator. The argument is not used.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In fact postfix is also not shown well since the returned object should, ideally, be const.
http://www.ishiboo.com/~nirva/c++/eff/MEC/MI6_FR.HTM
That's a good argument. It kills off the likes of the following, which I guess is a bad thing, but can be handy sometimes.

It's hard enough being politically correct without always being const-correct ;-)
#include <iostream>
#include <string>
using namespace std;
 
struct Foo {
	string name;
	Foo(const string& name) : name(name) {}
	Foo operator++(int) {
		Foo copy(*this);
		name += " (post-incremented)";
		return copy;
	}
	Foo& operator++() {
		name += " (pre-incremented)";
		return *this;
	}
	void mutate() { // Not a const function!!
		name += " (mutated)";
	}
};
 
ostream& operator<<(ostream& os,const Foo& foo)
{
	return os << "Foo[" << foo.name << ']';
}
 
int main()
{
	Foo foo("rabbit");
	Foo& foo_of_old = foo++;
	foo_of_old.mutate();
	cout << "Old mutated is " << foo_of_old << " and foo is now " << foo << '\n';
}

Open in new window

To add from my side:
Pre-fix is more efficient than post fix since there's an additional object being created in the post fix form to store the current value.
Pre-fix works on the invoking object itself.

Also the prototype for  postfix from the previous example stands corrected as below:

const Foo operator++(int);

This is because you want to prevent expressions of the below form:

i++++;

this will result in an erroneous value being computed. So to prevent this we make the return of operator++(int) constant.  Since operator++(int) is not a const member function it cannot operate on a constant object.
Avatar of lzha022

ASKER

Thanks everyone for answering this question. Before i read every post in detail, i will tell you where i got this question from. The other day i got an interview for a c++ job, they gave me this question for the test. I had no idea how to implement this. And of course i failed the interview.
I used to learn Java programming from study, but i got a c++ job after study. Though i have been working here for 3 year, i did not learn much c++ from work - my current job is too limited to a certain area. So i want to learn more c++ and find another job where i can have the chance to learn more new things.
Sorry to hear about the interview, lzha022. You can do a lot of productive work with C++ without mastering stuff like this. Ultimately it *is* worth covering this ground, but in the interim it must seem like a Shibboleth.
Avatar of lzha022

ASKER

rstaveley,
Thank you for the reply. Actually without this interview, i will not know there are a lot c++ knowledge that i have not learnt.
This is a good website, i will go through all those questions.
 http://www.parashift.com/c++-faq-lite/
>> Actually without this interview, i will not know there are a lot c++ knowledge that i have not learnt.
You know, one good way to learn what you don't know is to try and take part in contributing to answering questions on EE. If you don't know an answer research and find it out. I've been a professional C++ programmer for over 10 years and every day I still learn something new. The only way to get better is to try and stretch yourself everyday.

Cheers,

Rx.
Avatar of lzha022

ASKER

>>You know, one good way to learn what you don't know is to try and take part in contributing to >>answering questions on EE. If you don't know an answer research and find it out. I've been a >>professional C++ programmer for over 10 years and every day I still learn something new. The only >>way to get better is to try and stretch yourself everyday.
Yes, you are right. Thanks for the help.
Cheers
lzha022
I got a great deal value out of http://www.parashift.com/c++-faq-lite/ a few years ago when I was cleaning up my act.

I wound up buying Marshall Cline et al.'s C++ FAQs as wood pulp too, because I like to get away from the screen.

If books are your thing too and you hit the search button for book lists for C++ in this TA, you'll find some good reading lists. Axter's list is hard to fault: http:Q_21025086.html#11310550. In particular, Meyers and Sutter make a good grounding.