Sign in to follow this  

operator overloading problem

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm attempting operator overloading for the first time but its not going well so far...
#include <iostream>

class Beast
{
public:
	Beast();
	Beast(int i);

	void operator++ () {++_health;}
	Beast operator+= (const Beast&);
	int GetHealth()const {return _health;}
private:
	int _health;
};

Beast::Beast(): _health(10)
{
}

Beast::Beast(int i): _health(i)
{
}

Beast Beast::operator +=(const Beast& b)
{
	return Beast(_health + b.GetHealth());
}

int main()
{
	Beast beast(3);
	++beast;
	++beast;
	Beast beast2(10);
	std::cout << "Beast1 Health: " << beast.GetHealth() << std::endl;
	std::cout << "Beast2 Health: " << beast2.GetHealth() << std::endl;
	beast += beast2;
	std::cout << "Merging Beast1 and 2..." << std::endl;
	std::cout << "Beast 1's new health: " << beast.GetHealth() << std::endl;

	system("pause");
	return 0;
}

beast2's health is not added to beast, I'm sure this is a simple thing to fix. Any help will be much appreciated :D

Share this post


Link to post
Share on other sites
Your compound assignment operator should increment the beast's health and then return the current instance as a reference, like so:

Beast& Beast::operator+= (const Beast& beast)
{
_health += beast._health;
return *this;
}

Please note, I strongly advise that you don't use operator overloading for manipulating the health, it's completely un-intuitive.

Share this post


Link to post
Share on other sites
Woah, hold up. Why are you using operator overloading here?
Incrementing an object of type Beast makes absolutely no sense. It has no logical meaning, and anyone reading the code (including you later on) will have no clue what's going on. operator+= is even worse; why should you be able to add one Beast to another?
I would second dmatter's advice. Make an IncrementHealth member function instead.

Cheers,
--Brian

Share this post


Link to post
Share on other sites
Not to intrude, but did you every stop to think that he's simply learning operator overloading without thinking of any practical use for it? One can simply experiment all they want to learn the ins and outs.

Just slow down before you sound upset.

Share this post


Link to post
Share on other sites
Firstly thanks for the code now it works :D.
Secondly well I was thinking of a merge beast to merge stats later on like in a few games but this isnt my current goal.
Thirdly thanks Justin you are right I was just fiddling round I didnt actually have a direct aim.

Share this post


Link to post
Share on other sites

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this