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
C++

Avatar of undefined
Last Comment
rstaveley

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
evilrix

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
rstaveley

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
evilrix

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
rstaveley

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
trinitrotoluene

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.
ASKER
lzha022

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.
rstaveley

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
lzha022

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/
evilrix

>> 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.
ASKER
lzha022

>>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
Your help has saved me hundreds of hours of internet surfing.
fblack61
rstaveley

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.