Original Post
I am working on the weapon system for my game (see here for details) and i ran into a problem. I have a Weapon class from which all other weapons are a subclass of. Since most weapons are vastly different and therefore have different variables, the Weapon class contains only the basic variables common to all weapons and methods common to all (like fire()). I also have a list containing an instance of each of those weapons. The idea is that when a player buys a weapon (they can buy and sell during gameplay), a new weapon created which is copied from the list and the player has a pointer to this new instance. This way each player has it's own instance that can be changed (i.e. by power-ups) but is initially the same as the "default". The problem is that the copy method in each subclass needs to be an overloaded one of the Weapon class, but the compiler thinks that i am creating a separate method because the signature is different. So how do i make each subclass have their own copy method that can be called in a generic sense? Perhaps there is a better way to design this. thanks
class Weapon {
public:
static std::map<String /*the weapon id*/, Weapon*> weapons;
virtual void copy(Weapon* w) = 0;
};
class MachineGun : public Weapon {
int fireRate;
...
void copy(MachineGun* w);
};
void MachineGun::copy(MachineGun* w) {
this->fireRate = w->fireRate;
// other machine gun specific variables
}