Skip to main content
gamedev.net
🔒 Locked

Data storage class

Started by Twinkle Jan 20, 2006 at 8:52 AM 1 replies 450+ views
Original Post
Twinkle
Twinkle
Hi, I'm been trying for a while to get a simple data storage class working, with respect to accessing the stored objects outside the class.
class Data
{
private:
	vector<Terrain*>		m_TerrainList;		// list of terrain objects

public:
	// constructor
	Data();

	// destructor
	~Data();

	// methods
	void AddTerrain(Terrain* pTerrain);
	void CleanUp();

	// getters
	vector<Terrain*> GetTerrainList() { return m_TerrainList; }
};
#endif
// constructor
Data::Data()
{
	// reserve plenty of terrain object space
	m_TerrainList.reserve(50);
}

// destructor
Data::~Data()
{
}

// add terrain object to data storage class
void Data::AddTerrain(Terrain* pTerrain)
{
	// add terrain object
	m_TerrainList.push_back(pTerrain);
}

// delete all stored data
void Data::CleanUp()
{
	// remove all terrain objects
	vector<Terrain*>::iterator iter;
	for (iter = m_TerrainList.begin() ; iter != m_TerrainList.end() ; iter++)
	{
		delete (*iter);
		m_TerrainList.erase(iter);
		iter--;
	}
}
I get this to output the number of terrain objects that I have added…
TCHAR Dest[20];
LPTSTR Format = TEXT("Terrain objects: %i");
StringCbPrintf(Dest, 20*sizeof(TCHAR), Format, g_pData->GetTerrainList().size());
TextOut(hDC, 15, 300, Dest, 20);
But I don't succeed accessing individual terrain objects, been trying with code like this:
size_t StringLength;
vector<Terrain*>::iterator iter;
iter = g_pData->GetTerrainList().begin();
StringCbLength((*iter)->GetName(), 10, &StringLength);
TextOut(hDC, 20, 400, (*iter)->GetName(), StringLength);
I am obviously missing something essential as (*iter)-> doesn't seem to operate on my added objects. Help is appreciated. Regards /twinkle ;)
current hobby project found in folder test02 @ http://www.norberg-gbg.com/stefan/testgame/
rip-off
rip-off
this line here:

Quote:

iter = g_pData->GetTerrainList().begin();


you are getting an iterator from a temporary vector. after that line the iterator is useless, the temorary has been destroyed.

you can make your method GetTerrainList return a reference, but that breaks encapsulation. you could move the code inside Data, if you can.

you could also provide operator[] for the data class, and a size method, or even just return iterators to the internal vector.

its unclear from your code what benefit there is of having a Data object as opposed to just having a vector whereever the "Data" object is.

eg

class MyClass{   private:      Data data;      vector<Terrain*> vec;   public:      void doSomethingData()      {        // use data.GetTerrainList()        // use data.AddTerrain()         // use data.CleanUp();      }      void doSomeThingVector()      {        // use vec directly        // use vec.push_back()        // use vec.clear()      }};
Twinkle
Twinkle
The idea is certainly to move code like the example into the Data class as there is no real benefit of having messy code outside the classes. This was more for testing and understanding.

Thanks,
/twinkle ;)
current hobby project found in folder test02 @ http://www.norberg-gbg.com/stefan/testgame/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.