Archived

This topic is now archived and is closed to further replies.

Inheritance Question

This topic is 5152 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

Pretty simple question: I got a stat class which handles stats, it''s used for item stats, player stats, monster stats, etc ... In my stats class I have all the operator overload''s I need, but if I made the stats class a base class for other classes how would I beable to do something like this without writing new operators for every derived class. CPlayer& operator +=(CStat& rhs)

Share this post


Link to post
Share on other sites
Without trying it, I would imagine this would work, provided you dont want any additional behaviour (which you could acheive using polymorphism and methods in the operator- but one question at a time). I could be wrong, but the code you''ve written looks ok to me.

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

Share this post


Link to post
Share on other sites
Here's an example
Stats Class
Public:
->getStr
->setStr
->getVit
->setVit
->operator +=
->operator -=

Private:
->Str
->Vit

Player Class: Stats class
public:
Operator += :Stats
Operator -= :Stats

Okay, yeah, my example is not in C++ but I hope you get the general idea.

Player1 += addstats;

I want to beable to add a stats to a player class without having to rewrite the operator overload for the stats class in the player class. Templated stats class maybe?

[edited by - PumpkinPieman on November 9, 2003 5:02:59 PM]

Share this post


Link to post
Share on other sites
Don''t do that. When you overload the addition operator, it should do addition. Math-style addition. As in, adding two equivalent things together and returning the result. Doing your own thing with operators makes your code hard to read and understand.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
The other option I have is to have the stats class as a variable, and when I want to do any operator, I make an operator for the player class, which will then pass on to the stats class. But, meh, I don''t know. I just don''t want to write code that can be avioded.

Share this post


Link to post
Share on other sites
That''s a lot better.

Here''s a slightly contrived example. Suppose that sometime down the road, you decide to add romantic interest to your game. So you give the Player a second Stats object, called "desiredStats", which represents the ideal stats of his dream date. Uh oh, now which Stats object do the += and -= operators operate on? Again, this is a contrived example, but the point is that if a Player HAS-A Stats, it should NOT act as if it IS-A Stats. It should expose the Stats it contains somehow, perhaps through a GetStats function, but it should not itself pretend to be the Stats, which is what the += and -= operators would do.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites