Thread: class inheritance & vectors

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005

    Talking class inheritance & vectors

    Hey, i'm making my final text rpg before I move onto direct x and i'm making great progress. I have created an inventory class and a vetor to hold Weapon objects. Now, the question I have to ask is: If I have created multiple classes that inherit all of Weapon's data members and functions(classes would include Sword, Bow, and Staff), would I be able to fill the vector with those classes?

    vector<Weapon*> m_Weapons;
    void addW(Weapon* pWeapon);
    Inventory.addW(Sword)<-i have no idea how i would add in a sword object, would i have to cast?  I really don't know...

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Chch, NZ
    for this kind of thing you really want either a boost.ptr_vector or a container or boost.shared_ptr's.

    your code would look something like

    class Weapon
    virtual void Strike(void) = 0;
    class Sword : public Weapon {};
    class Staff : public Weapon {};
    class Bow : public Weapon {};
    class ThermoNuclearWarhead : public Weapon {};
    boost::ptr_vector<Weapon> m_weapons;
    m_weapons.push_back(new Sword);
    m_weapons.push_back(new Staff);
    m_weapons.push_back(new Bow);
    m_weapons.push_back(new ThermoNuclearWarhead);
    then you can use boost.bind to do even cooler stuff like
    // call Strike() on each weapon polymorphically...
    std::for_each(m_weapons.begin(), m_weapons.end(),
                          boost::bind(&Weapon::Strike, _1);
    and that is the power of C++, mixing functional and OO techniques.

    edit: fixed code tags
    Last edited by ChaosEngine; 10-12-2006 at 07:07 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Nice, ChaosEngine. I didn't know such things could be done...

  4. #4
    Registered User
    Join Date
    Jan 2005
    >> i have no idea how i would add in a sword object, would i have to cast? I really don't know...
    No, you don't have to cast. Just past a pointer to a Sword if you want to add a Sword. The add function will work fine.

  5. #5
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005

    Thumbs up

    thanks alot! I'll be sure to post the game in the games forum once it's complete.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2008, 12:10 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Inheritance with pointers:Overloading?
    By katie in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2004, 01:26 PM
  4. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 10:56 AM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM