I am developing a very small and simple game engine in C ++. I have a Creature class and a Weapon class. A Being must know what weapon (s) it has, and every Weapon must know a Being that has it. The main reason I would like the Weapon to know what this Creature is is to prevent it from destroying its own Creature (when the Weapon attacks, it detects all the Creatures it encounters and kills them, except for the Creature that has Weapon).
Here's what the (simplified) creature class looks like:
class Creature {
private:
Weapon* weapon_;
public:
void SetWeapon(Weapon* w){
weapon_ = w;
w->creature_ = this;
}
}
Here's what the (simplified) weapon class looks like:
class Weapon{
friend class Creature;
private:
creature_;
}
As you can see, I made the Creature a Friend Weapon class so that the creature's setWeapon () method can set the pointer of the private creature_ appropriately.
:
, , , . , ? "", , , , ( ). , , ? Map/Entity, Inventory/Item .. , , "Container" "Item", . , , BOTH (.. , , ). , ?
? , ?
, ?
. , , , .