Archived

This topic is now archived and is closed to further replies.

DataClass - Storing any type - any number

This topic is 5653 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

Hail I''m working on a class that can store any datatype defined. The big thing with this dataclass it that it have to handle unknown number of items. To do this I create two Item pointer, DataItemPointer will point to a dynamic memory location. When this dynamic memory array is full I need to allocate more. I allocate with DataItemPointerTemp more memory, and then redirect the DataItemPointer to the new memory array. I now realizes that this isn''t a good idea. That''s allot of memory to allocate after awhile. But I cant think of a better way. I don''t want a Linked list, because I want to access the array for processing. With a linked list you have to remove a item before you can access the next. Or you can do it....but it will be long addresses. I want to copy the PlanetData adresses in the old DataItemPointer array to the new DataItemPointerTemp array. New 5 array empty Old 2 array Pointer FULL ==DataItemPointerTemp== ==DataItemPointer== Array PlanetData PlanetData [Adress 2] [NULL][NULL] [Adress 0] [Xpos:3][Ypos:44] [Adress 3] [NULL][NULL] [Adress 1] [Xpos:85][Ypos:23] [Adress 4] [NULL][NULL] [Adress 5] [NULL][NULL] [Adress 6] [NULL][NULL] This is getting complicated, but if I redirect the DataItemPointerTemp array to the DataItemPointer there will be some dynamic allocations left not deleted. Humm..... I need to copy the data inside the PlanetData struct instead. Does anyone have a good idea how I can do this....? If you dont understand what I mean post it!!
  
//Class def

#ifndef DataClass_H
#define DataClass_H

struct PlanetData
{
	int iXPOS;
	int iYPOS;
};

typedef PlanetData Item;

class DataStorage
{
	private:
		
		unsigned int iItems;
		unsigned int iMAXITEMS;
		
		
		
	public:
	
	
		DataStorage()
		{
			iItems					= 0;
			iMAXITEMS				= 0;
			DataItemPointer			= 0;
			DataItemPointerTemp		= 0;
		};
		~DataStorage();
	
		void add(int iXPOS,int iYPOS);
		int NumItems();
	    
		//Data Object Pointer

		Item *DataItemPointer;			
		Item *DataItemPointerTemp;
};
#endif
  

  
//Class Functions

#include "DataClass.h"

int DataStorage::NumItems(){return iItems;}

void DataStorage::add(int iXPOS,int iYPOS)
{
			
		if(( iMAXITEMS - iItems ) == 0) 
		{
			iMAXITEMS+=10;
		
			DataItemPointerTemp = new Item[iMAXITEMS];
                         //Some kind of redirection :)

			for (unsigned int i=0;i <= iItems; i++)
			*DataItemPointerTemp[i] =  &DataItemPointer[i];
			
			DataItemPointer = DataItemPointerTemp;
		}
		else
		{
			DataItemPointer[iItems].iXPOS = iXPOS;
			DataItemPointer[iItems].iYPOS = iYPOS;

		}
				
		iItems++;
}

DataStorage::~DataStorage()
{
	//Delete stuff here....

}

  

Share this post


Link to post
Share on other sites
I didn't read through all of your code, so I don't know if this will work for you, but have you looked into vectors? They'll handle any data type and can increase/decrease to suit you. They also can be accessed as arrays. They're part of the STL, so you should be able to find lots of info on them. They're real easy to use. They work like a stack.

vector v; // the part in brackets could be any data type
v.push_back(1);
v.push_back(2);
v.push_back(3);
for (int i=0; i<v.size(); i++)
{
cout << v << endl;
}
v.pop_back(); // removes last element (3)


They have a bunch of other method's also to manipulate them. Well, I hope that will work for you.

- David

[edited by - BitBlt on June 24, 2002 2:34:59 PM]

Share this post


Link to post
Share on other sites
Do you know what templates are? If not read up on them, i believe what your trying to do could be accomplished with them. Otherwise what you could also do if the data to be stored is derived from a single base class is to store an array of that base class and then dynamic_cast them to the class they should be with RTTI...

tell me how it works out...

thanks

-jonnii

Share this post


Link to post
Share on other sites
All I'm gonna say is look into templates bro. Templates.

Avast ye cantankerous rapscallions, I was all set to be big and bad with my templates answer and you both beat me to it. DAMN THE BOTH OF YOU! DAMN YOU!

[edited by - Funkymunky on June 24, 2002 2:39:20 PM]

Share this post


Link to post
Share on other sites