1

I am writing a text based RPG, and I have three classes that inherit from a super class, they all have special attacks that they can perform, at the same time I have a class that holds the function which handles battles in my game.

Now how do I get the unique special abilities functions for whatever role the player chooses into the battle function?

Also I am using the vector.push_back method to handle how my sub classes are referenced.

1
  • Have you seen circlemud.org? Commented Apr 1, 2012 at 1:30

1 Answer 1

2

You just need a virtual member function in your superclass:

class Superclass
{
    virtual void specialAbility() = 0;
};

And implementations in your subclasses:

class Subclass1 : public Superclass
{
    void specialAbility()
    {
        // Do something specific to this subclass...
    }
};

class Subclass2 : public Superclass
{
    void specialAbility()
    {
        // Do something specific to this subclass...
    }
};

Now when you do your battles, you could do something like this (edited to put in class):

class BattleDoer
{
    void doBattle(std::vector<Superclass*> combatants)
    {
        for (unsigned int index = 0; index < combatants.size(); index++)
        {
            combatants.at(index)->specialAbility();
        }
    }
};

This will cause the appropriate member function defined in the subclass to be called on each of the combatant objects.

Edit:

To populate your combatant list and call the doBattle() function, you can do something like this:

int main()
{
    Superclass* combatant1 = new Subclass1;
    Superclass* combatant2 = new Subclass2;

    std::vector<Superclass*> combatants;
    combatants.push_back(combatant1);
    combatants.push_back(combatant2);

    BattleDoer battleDoer;
    battleDoer.doBattle(combatants);

    delete combatant1;
    delete combatant2;
}

It is important to use pointers here. Only pointers and references can be used to provide polymorphism. You cannot have variables of type Superclass because it cannot be instantiated, you can only have references and pointers of type Superclass. Smart pointers should probably be used but I thought I'd keep it simple.

4
  • If the vector is defined in main, and the void doBattle function is defined in a battle class, how do i code the call in main so i can pass the vector Commented Mar 31, 2012 at 16:35
  • I've edited my answer, hopefully it helps. Commented Mar 31, 2012 at 20:48
  • Just a small nit: you have a memory leak. When a vector falls out of scope it calls the destructor of each object of the contained Type. Calling ~Type when Type is a Type* is a non-op. You must manually delete each object first.
    – Casey
    Commented Apr 1, 2012 at 17:56
  • Thanks @Casey, I've added the deletes. I usually use smart pointers so I'm not used to manual deletion. Commented Apr 1, 2012 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.