Jump to content
  • Advertisement


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


DataClass - Storing any type - any number

This topic is 6019 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
		unsigned int iItems;
		unsigned int iMAXITEMS;
			iItems					= 0;
			iMAXITEMS				= 0;
			DataItemPointer			= 0;
			DataItemPointerTemp		= 0;
		void add(int iXPOS,int iYPOS);
		int NumItems();
		//Data Object Pointer

		Item *DataItemPointer;			
		Item *DataItemPointerTemp;

//Class Functions

#include "DataClass.h"

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

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

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


	//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
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...



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
Okey i will take a look on vektors.

This will explain some...!!

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!