core::list with own class

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Epi
Posts: 9
Joined: Wed Mar 04, 2009 4:27 am

core::list with own class

Post by Epi »

hey guys,

i'm using the core::list and have a litte problem with the pointers. i have 2 classes the first one is a planet and the second one is a planetlist.

here is the CPlanetList.h:

Code: Select all

class CPlanetList
{
private:
	core::list<CPlanet> pList;
	core::list<CPlanet>::Iterator it;
public:
	CPlanetList()
	{
		it = pList.begin();
	}

	void FrameControl()
	{
		for(it = pList.begin(); it != pList.end(); ++it)
			it->Orbit();
	}
	
	void AddPlanet(CPlanet &planet)
	{

		pList.push_back(planet);
	}
};
and here comes the stuff from main():

Code: Select all

//PlanetList
CPlanetList Planets;
//Planets
CPlanet MySun;
MySun.Init(smgr, 0, 8, core::vector3df(0,0,0));
...
		
CPlanet MyPlanet;
MyPlanet.Init(smgr, 1, 5, core::vector3df(30,0,30));
...

Planets.AddPlanet(MySun);
Planets.AddPlanet(MyPlanet);
the initstuff in main() works fine and it gives a reference to CPlanetList::AddPlanet(), but then the push_back function creates a new instance instead of just passing over the reference.

does anyone know how to solve this? i want it to be the same object in the list as i created before in main()?

thanks
wITTus
Posts: 167
Joined: Tue Jun 24, 2008 7:41 pm
Location: Germany

Post by wITTus »

That's because your list does store <CPlanet> and not a reference or a pointer to the existing object (like <CPlanet*> for example). So when you call push_back(), the copy constructor gets invoked.
Generated Documentation for BlindSide's irrNetLite.
"When I heard birds chirping, I knew I didn't have much time left before my mind would go." - clinko
Epi
Posts: 9
Joined: Wed Mar 04, 2009 4:27 am

Post by Epi »

oh jeah, thanks

here's the working code:

Code: Select all

class CPlanetList
{
private:
	core::list<CPlanet*> pList;
	core::list<CPlanet*>::Iterator it;
public:
	CPlanetList()
	{
		it = pList.begin();
	}

	void FrameControl()
	{
		for(it = pList.begin(); it != pList.end(); ++it)
			(**it).Orbit();
	}
	
	void AddPlanet(CPlanet *planet)
	{

		pList.push_back(planet);
	}
};
Post Reply