Jump to content
  • Advertisement
Sign in to follow this  
kzar

Different inherited class objects in same list?

This topic is 4699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Suppose I have a class called cEntity as the basic entity class and then cOgre, cPlayer and cTrap all inherited from cEntity. Would it be possible to make a list that could store instances of all the different class's like ogre and trap mixed up together? Thanks

Share this post


Link to post
Share on other sites
Advertisement
You could make a list that could store (smart) pointers to cEntity, which could then store pointers to cOgre, cPlayer and cTrap.

Share this post


Link to post
Share on other sites
In C++ or Java, yup. You'll have to downcast to the appropriate type when you take it out of the list though. Otherwise you only get the methods of the base class.

Share this post


Link to post
Share on other sites
I typed this just now into the post so ovbiously its not bug free, but would this kind of idea work do you think? Thanks


class cEntityManager
{
public:
add_entity(char type);
update();
draw();

private:
list <cEntity> entity_list;
};

void cEntityManager::add_entity(char type)
{
if (type == 1)
{
cOgre new_ogre(blah, blah, blah);
entity_list.push_front(new_ogre);
}
if (type == 5)
{
cHovercraft new_craft(blah, dee, blah);
entity_list.push_front(new_craft);
}
}

void cEntityManager::update()
{
list <cEntity> ::iterator current_entity;
for(current_entity = entity_list.begin(); current_entity != entity_list.end(); ++current_entity)
{
current_entity->update();
}
}




Quote:
Original post by LockeCole
Don't forget to read up on virtual.


Yea thats my idea, say having a virtual function update() and draw() for different entity types so they do different stuff.

Share this post


Link to post
Share on other sites
No. That will cause object slicing: only the base part of the derived objects will be copied into the list.

Share this post


Link to post
Share on other sites
well not the way you want

try



class cEntityManager
{
public:
void add_entity(cEntity *type); // shared_ptr<Entity>&
void update();
void draw();

private:
list<cEntity*> entity_list;
};

void cEntityManager::add_entity(cEntity *type)
{
entity_list.push_front(type);
}

void cEntityManager::update()
{
list <cEntity*> ::iterator current_entity;
for(current_entity = entity_list.begin(); current_entity != entity_list.end(); ++current_entity)
{
/*
whoops.
i forgot the *
*/

(*current_entity)->update();
}
}






they have to be pointer otherwise polymorphism wont work ( they could be refernces, but you'd still need to store the referees, and his could easily cause a mess... )

[Edited by - rip-off on January 4, 2006 4:16:41 PM]

Share this post


Link to post
Share on other sites
I kind of get what you meen but how come you made add_entity() take an cEntity pointer instead of a char?

Share this post


Link to post
Share on other sites
Quote:
Original post by kzar
I kind of get what you meen but how come you made add_entity() take an cEntity pointer instead of a char?


cause i was going to mention factories but then decided not to, since that is basically what your doing. it doesnt matter.

good luck!

Share this post


Link to post
Share on other sites
I have roughly coded most of the stuff I was thinking of now but I am getting an odd error.

"entity.cpp:33: error: request for member `update' in `*(¤t_entity)->std::_List_iterator<_Tp>::operator-> [with _Tp = cEntity*]()', which is of non-class type `cEntity*'"

(line 33 is the current_entity->update(); line neer the bottom here)


class cEntity
{
public:
cEntity();
virtual void draw(SDL_Surface *screen);
virtual void update();

protected:
int x, y;
string current_sprite;
std::map<char *, cSprite *> sprite_map;
};

class cEntityManager
{
public:
void add_entity(char type);
void update();
void draw(SDL_Surface *screen);

~cEntityManager();

private:
list <cEntity*> entity_list;
};

void cEntityManager::update()
{
list <cEntity*> ::iterator current_entity;
for(current_entity = entity_list.begin(); current_entity != entity_list.end(); ++current_entity)
{
current_entity->update();
}
}




Any ideas? Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!