Archived

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

vaneger

vector help

Recommended Posts

is it possible to have a vector that simply stores data regardless of its type? liek for instance vector[0] is an int vector[1] is a char and vector[2] is a *long ?

Share this post


Link to post
Share on other sites
If you really want to make a strong system, design a class that can allocate a block of memory and keep list of what types are at what memory positions. For example, the list of types could look something like this:

Type | Position
-----------------
int 0
float 4
char 8
long* 9
int 13
char 17
char 18
int* 19


Something like that. Or you could simply create a list of void pointers and allocate the memory for each type yourself, and cast everything, but you still would need some sort of reference for what datatypes are stored. Actually, it depends how you are using it. If you need the datatypes, then you need to store that information, otherwise you can cast everything.

EDIT: Might I ask what you need to do this for?


[edited by - Zipster on July 29, 2002 9:14:50 PM]

Share this post


Link to post
Share on other sites
If you are storing different data types for a vectors components then maybe it's not a vector anymore. Perhaps you should be using some sort of other data structure.... Anyway, of you really want to:

If you want the all the vectors components to be of the same type then you can probably use templates:

        
template <typename T>
struct Vector
{

T x,y,z;
};

//you can then instantiate the template with:


Vector<int> v; //this will create a vector will int components

//also remember to initialize all the components (x,y,z)



or if you want each component to be a different data type:

  
template <typename A,typename B,typename C>
struct Vector
{

A x;
B y;
C z;
};

//you can then instantiate the template with:


long test = 3415;
Vector <int,char,long*> weirdV;
weirdV.x = 2;
weirdV.y = 'c';
weirdV.z = &test;




[edited by - bslayerw on July 29, 2002 12:23:30 AM]

Share this post


Link to post
Share on other sites
well using templates is one way but thats annoying to have to go vector etc and i have a templated vector class any way. what i was thihking of trying is sounding more like zipsters idea of void pointers though ive never doen such a thing so coudl u post some code on that? i have a method of getting the data type using tyeinfo.h so thats no problem. the reason i wanted to do this is so i could create a singel vector with a few similar data types in it and then parse them as needed using typeinfo to get its data type then calling the apporiate functions

Share this post


Link to post
Share on other sites
from my other web browser window:

http://www.boost.org/libs/any/index.html



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites