-
March 19th, 2009, 03:34 PM
#1
A question on Pointers.
I am not pretty sure if I should post this here.
Well another query for the day. A bit long post this time. Please bear with me and help me
I have a class called TEnt which basically defines a entity.
I have another class called Manager which sort of manages the entity. The manager is a singleton class which has the following members
Code:
/*! This is called from other classes to register the instance with the manager*/
void REGISTER_ENTITY_INSTANCE(TEnt& instance);
/*! Call the set entity call in each of the registered entities*/
void EntityCall(void);
/*! Calls a draw entity call to each of the registerdd entity*/
void drawEntities(void);
/*! Prints out a test message*/
void print();
private:
/*! A vector of TEnts*/
std::vector < TEnt* > m_ppTEntList;
///////////////////////////////////////////converting this into a singleton
////////////////////////////////////////////////////////////////////////////
private:
EntityManager(){;}
EntityManager(const EntityManager&){;}
EntityManager& operator= (const EntityManager&){;}
static EntityManager* m_pInstance;
static bool is_InstanceSet;
public:
static EntityManager* getPInstance();
~EntityManager()
{
is_InstanceSet=false;
}
I have a Screen class which displays all the entities on the screen and a LevelEditor class which initialises and registers the entities on to the entity manager.
My Level Editor Class has the following function
Code:
void LevelEditor::Init_Level(){
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
}
The REGISTER_ENTITY_INSTANCE(a) pushes the address of the instance onto a vector.
Code:
m_ppTEntList.push_back(&instance);
and my screen class has this
Code:
//Init level
LevelEditor* Levels=new LevelEditor;
Levels->Init_Level();
//Init the Emanager
EntityManager* EManager=EntityManager::getPInstance();
This piece of code initialises the level and gets a instance of the Manager.
Now This is the error I am facing
When I call the print function in the screen class it works fine .
it says that a entity has been registered and that the size of the vector is now 1. But this is the piece of code that seems to go wrong and creates a SEGMENTATION fault.
Code:
vector<TEnt*>::iterator iter;
for (iter=m_ppTEntList.begin();iter!=m_ppTEntList.end();iter++)
{
(*iter)->my_entity_paint();
}
All I wanted to do was access every element of the vector and then call a function of each of the element.
Can any one point me to where I am wrong or any better way at all to how this can be done.
-
March 19th, 2009, 03:42 PM
#2
Re: A question on Pointers.
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
-
March 19th, 2009, 03:46 PM
#3
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
-
March 19th, 2009, 04:01 PM
#4
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
-
March 19th, 2009, 04:20 PM
#5
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.
-
March 19th, 2009, 04:31 PM
#6
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
-
March 19th, 2009, 04:43 PM
#7
Re: A question on Pointers.
Can you please give me a example of as to how I should be doing it?
-
March 19th, 2009, 04:52 PM
#8
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
-
March 19th, 2009, 05:13 PM
#9
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?
-
March 20th, 2009, 03:24 AM
#10
Re: A question on Pointers.
you can use a pointer to the base class to access the derived.
Code:
std::vector<baseentity*> bvec;
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|