So i'm trying to write a class with all the functions same as vector. I know i can use the vector template but i just want to give myself a little challenge.
Ok, vector takes classes, but my i intent to make it take only data type (char, string, int, float....). Because i will need "push" function to add stuff into the array, so i started like this:
Code:
struct link
{
void* dataType;
link* next;
};
Then i start out with a class:
Code:
class myVector
{
private:
link* base;
public:
myVector(){ base = NULL; }
void Push(void* data)
{
link* var = new link;
var->dataType = data;
var->next = base;
base = var;
}
void Display()
{
link* var = base;
while(var != 0)
{
cout<<var->dataType<<endl;
var = var->next;
}
}
};
So this way, i can only display the memory of the data type (or whatever it is) when i push in. If i want to actually display the variable, i have to use
Code:
(variable type*)var->dataType;
So is there anyway i can convert from void* to whatever data type the users want when they declare the class. like this:
Code:
public:
void* variable_need_to_convert;
myVector( user's data type)
{
//convert the variable_need_to_convert into this data type
}
//Then
void Display()
{
cout<<(converted data type) data<<endl;
}