Skip to main content
gamedev.net
🔒 Locked

Inheritance

Started by daniel_i_l Aug 7, 2006 at 12:54 PM 3 replies 500+ views
Original Post
daniel_i_l
daniel_i_l
Lets say I have a base class: soldier, and inherited from it: scout, and private. Is there a way that I could hold a vector of scouts and a vector of privates and update eachone with it's own functions and everything, but also hold a vector of all the soldiers to do common things like rendering(with a virtual function)? I know that I could make three seperate vectors - one for each task- but that would be messy and sortof defeat the purpose of the infheritance. what is the common "elegant(but simple)" way to do this? -note: I used the vectors just for an example, probably there is a better way. It's sortof hard for me to express this question, what I'm basiclly asking is how do you utilize the fact that with the base class you can access all the inherited children? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Aardvajk
Aardvajk
Rather than store seperate vectors as you describe, you would be better to have a virtual method in the soldier interface that was overridden by scout and private to do their specific stuff.

I suppose once you have constructed a vector of solidiers, you could run through and create seperate lists of scouts and privates:

vector<soldier*> sols;vector<scout*> scouts;void f(){    for(int i=0;i<sols.size();++i)        {        scout *s=dynamic_cast<scout*>(sols);        if(s) scouts.push_back(s);        }}


But it seems like a bit of a hack to me, plus you would have to ensure that the memory addresses of the solider vector items never change, or the scout list will become invalid.

When I have polymorphic game entities, I generally have a virtual TakeTurn function or whatever that gets called for everything and do any item specific stuff in there.

HTH Paul
Sol462
Sol462
I'm not 100% sure of what you're asking (That, and my coffee just ran out :-), but yes it is possible.
If a class inherits from a base class, then they can be in the same vector. For example in my old OpenSG wrapper I had a Mesh class that inherited from Entity. I also had a vector that contained both Entities and Meshes and it worked out fine.
tendifo
tendifo
This is what I would do. No casting is needed.

Edit: Hmm.. well below is good, but you might wanna have two more vectors for just CScout and CPrivate. When you push_back, you push the same pointer to the everybody vector and the C*** specific vector.

class CSoldier{public:    virtual void update();    virtual void draw();};class CScout : public CSolder{public:    void update();    void draw();};class CPrivate : public CSoldier{public:    void update();    void draw();};vector<CSolider*> everybody;int main(){    for (int i = 0; i < (int)everybody.size(); i += 1)    {        CSoldier *s = everybody.at(i);        s->update();        s->draw();    }    return 0;}

Topic Locked

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

Sign in to reply to this topic.