-
Um, theyre as good as any, but thanks for the tip on the reference thing i forgot totally about that.
I thought that when you called the attack function and did something like this...
Code:
class Creature
{
public:
Creature(int health, int attack, int defense)
{
statistics.Health = health;
statistics.Attack = attack;
statistics.Defense = defense;
}
struct Stats
{
int Health;
int Attack;
int Defense;
};
void Attack(Creature c)
{
c.statistics.Health -= statistics.Attack;
}
Stats statistics;
};
class Human : Creature
{
};
int main()
{
Creature C1(100, 25, 10);
Creature C2(100, 25, 10);
C1.Attack(C2);
return 0;
}
it would work as planned
Guess not...
-
why do you not have your attack function return something, so you can know if it was successful? Also, what I did was not any different than a nested struct it just kept that little bit of clutter out of the class. Although it is all really in preference. That and if you like to have multiple scope levels in your class.
I think that
Code:
bool Attack(Creature& target);
{
if ( C1.Attack(C2) == false )
return ERROR;
}
this would be a little more "logical" in such a situation. That and it help to keep bugs narrowed down.
Also, read some books. The patience of people on these boards has been limited these last few months. I know you used to know alot, but now just start over, and dont try to get in over your head. I know this is flame bait. Also, there was a "&" reference symbol in my previous post but there must have been a Copy/Paste error
-
>Nah stats inheriting creature is illogical, because creatures have stats, but are not stats themselves.
Actually, if they share all the same properties, then a Creature is a Stat
Also, you generally shouldn't have mutable public data members in a class. It basically negates the purpose for having the class in the first place. But you probably already knew that.
-
>> Actually, if they share all the same properties, then a Creature is a Stat
If they share the same interface then a Creature is a Stat. However, in this case Creature is-implemented-in-terms-of Stats is likely a more accurate description of the relationship, which is why composition should be preferred.