Skip to main content
gamedev.net
🔒 Locked

Overloaded method in subclasses

Started by XTAL256 4 replies 950+ views
Original Post
XTAL256
XTAL256
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".

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
}

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
[Window Detective] - Windows UI spy utility for programmers
JarkkoL
JarkkoL
You should use prototype pattern for this. The base class declares clone() which returns pointer to the Weapon class:
class Weapon{public:  virtual Weapon *clone() const=0;};

then in derived classes you implement it like:
class MachineGun: public Weapon{public:  virtual Weapon *clone() const  {return new MachineGun(*this);}};

and voila, you got new copy of your weapon object.

[Edited by - JarkkoL on March 18, 2009 6:40:02 PM]
Aardvajk
Aardvajk
Just to expand on JarkkoL, you can actually do this:

class Weapon{public:    virtual ~Weapon(){ }    virtual Weapon *clone()=0;};class Gun : public Weapon{public:    virtual Gun *clone(){ return new Gun(*this); }};class Rifle : public Weapon{public:    virtual Rifle *clone(){ return new Rifle(*this); }};


In this specific case, derived class implementations of virtual methods are allowed to differ in their return type, as long as the type is a reference or pointer to their own type or the type of a parent class. Covariant return types.

It is useful as if you are calling Rifle::clone() through a Weapon*, you get a Weapon* back but if you are calling it through a Rifle*, you get a Rifle* back so you don't need to cast if it is already "known" what the subtype is.

void a(Weapon *w){    Weapon *x=w->clone(); // return Weapon*    delete x;}void b(Rifle *r){    Rifle *x=r->clone(); // returns Rifle*    delete x;}void f(){    Rifle r;    a(&r);    b(&r);}
XTAL256
XTAL256
Thanks for your help guys, i'll have a more in-depth read of the links when i get home.
[Window Detective] - Windows UI spy utility for programmers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.