Re: A question on Pointers.
Quote:
Originally Posted by
sundar0206
The REGISTER_ENTITY_INSTANCE(a) pushes the address of the instance onto a vector.
Code:
m_ppTEntList.push_back(&instance);
Just guessing here. Are you shure instance doesn't go out of scope after that.
Kurt
Re: A question on Pointers.
I am not exactly sure as to what you mean by going out of scope!! ;)
But it seems to register it because when I test the size of the vector from the screen class I dont get a error and it says the entity has been properly registerd
Re: A question on Pointers.
i was referring to sth like this
Code:
void createInstance() {
entity a;
REGISTER_ENTITY_INSTANCE(a);
}
In such a case the entity a would go out of scope after return from createInstance() and you would have added a dangling pointer to the vector.
Kurt
Re: A question on Pointers.
I seem to have gone wrong with the pointing but I think what you said is probably the cause of the problem.
This is from a a level editor class that I use to register the entities on to the manager.
Code:
void LevelManager::InitLevel(){
//A Instance of Entitymanager to which all the instances are registered
vec3d pos;
pos.Initialize(10,0,0);
TEnt a(pos);
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
}
and this is from the screen class
Code:
LevelEditor* Levels=new LevelEditor;
Levels->Init_Level();
EntityManager* EManager=EntityManager::getPInstance();
EManager->drawEntities();
The above code throws up errors and causes segmentation faults.
But if i totally take off the level editor and put everything into the screen class like
Code:
EntityManager* EManager=EntityManager::getPInstance();
vec3d pos;
pos.Initialize(3,0,3);
TEnt a(pos);
EManager->REGISTER_ENTITY_INSTANCE(a);
EManager->drawEntities();
This doesn't......
Is this due to the error which was told in the last post
But I want to have a seperate Level Editor class which I can integrate with a GUI.
Re: A question on Pointers.
You will have to dynamically create the TEnt in InitLevel().
If you would use a vector of smart pointers to register the entities you don't have to worry to delete the entities.
Kurt
Re: A question on Pointers.
Can you please give me a example of as to how I should be doing it?
Re: A question on Pointers.
Code:
void LevelManager::InitLevel(){
//A Instance of Entitymanager to which all the instances are registered
vec3d pos;
pos.Initialize(10,0,0);
TEnt * a = new TEnt(pos);
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
// REGISTER_ENTITY_INSTANCE should take a TEnt *
}
Kurt
Re: A question on Pointers.
Thanks a lot Kurt...
Works great now ;))
Is it fine if I ask you some thing else??
This is another problem I have.
I got a following inheritance hierarchy.
TEnt, Male and Female inherits from a baseentity.
What I want is my Entity Manager class to access all the instances of all the three types of entities.
Currently I have a vector of pointers to each of the individual entities which i register in the register function
Code:
std::vector < TEnt* > m_ppTEntList
void REGISTER_ENTITY_INSTANCE(TEnt* instance);
This would mean that I would have to declare a vector one for each of the child classes and if I increase the class count I would propotionally keep increasing. Also the register and any other function that takes in a input needs to be repeated.
I know this part is bad coding
Can this be avoided?
Re: A question on Pointers.
you can use a pointer to the base class to access the derived.
Code:
std::vector<baseentity*> bvec;