Jump to content
  • Advertisement
Sign in to follow this  
Twinkle

Data storage class

This topic is 4537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ;)

Share this post


Link to post
Share on other sites
Advertisement
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()
}
};

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!