A Query on pointers and inheritance

Hi guys,

I am doing a project for my mid term and I am held up with this doubt. I might have misunderstood something all together or it might be a small mistake. Please point it out:

I have a baseentity class defining the features of a character.
I have three entities TEnt, Male and Female which inherits from the base entity.
Now what I wanted was to have a Entitymanager Class which manages all the entities.

This is a code I wrote for my entity manager:
namespace Sims{
/*! Forward declaration of the class*/
class TEnt;


/*! class Entity manager inherits from the singleton_client class */
class EntityManager:private Singleton_client{
/*! \class Sims::EntityManager "include/EntityManager.h" <BR>
\brief A Singleton manager entity that basically manages all the entities in the scene
Inherited from the SEntityEyeDefs
*/
public:
/*! A class that registers the entity to the base vector*/
/*! FIXME: This function needs to be defined for every class of entity*/
void REGISTER_ENTITY_INSTANCE(TEnt& instance);




/*! Prints out a test message //TODO: needs to be removed after testing */
void print();

private:
/*! A vector of Each of the classes */
//FIXME: This piece of code is rubbish.. i need to have a access to all the entities rather than a vector of every set of entity
/*! A vector of TEnts*/
std::vector < TEnt* > m_ppTEntList;

};
}

As you can see here there needs to be a private member variable for every class that inherits from the base entity . Also Every function like the REGISTER which has a input or output of the class needs to be called for each and every class. I can understand that this is bad coding. Is there a better way to do this at all?
Assuming TEnt is a base class of Male and Female.
I don't think its bad coding, your manager class is a typical Container of TEnt objects .Of course it can hold all childern of TEnt too. Even, the class EntityManager can be defined as a class template to store objects of various types ( not TEnt only).
Concerning access to the private container items, you need to define access function. But it is a typical activity when encapsulating private data. I recommend to continue the way you started.


It sounds like you could have a vector of BaseClass pointers in your manager class. In the BaseClass constructors it can add itself to it and in the BaseClass destructor remove itself.

The result will be a single manger instance with a vector of pointers to all of your BaseClass instances (regardless of what derived instances they really are) and you could loop through the container and call methods defined in BaseClass for each of them.
Actually TEnt, Male and Female classes are derived from the base class base entity.

hi seymore. is it possible to give me a small example of how it should be done coz i seem to be confused !!
Topic archived. No new replies allowed.