I am trying to implement a mixed list, so for example, I could do this:
mylist* l= new mylist();
l.push_back<int> (4);
l.push_back<string> ("hello");
And this exercise, therefore, is not a valid solution for using other libraries, such as boost. This is a class with a few more methods:
template <class T>
class node
{
private:
void* next;
void* prev;
T data;
public:
node(T data)
{
this->data=data;
}
template <class R>
void link_to (node<R>& other)
{
next=&other;
other.prev=this;
}
};
Because I don’t know how to control the fact that with the help of void pointers I cannot distinguish the data pointed to by this class. With dynamic_cast, I have to try all types (node, node, etc.) ....), so this is not an acceptable solution. So, for example, if I want to print a series of nodes, I cannot do this:
int main(int argc, char** argv)
{
node<int> a(1),c(2);
node<std::string> b;
a.linkTo(b);
b.linkTo(c);
std::cout << a.data;
void* ptr=a.next;
cout << ptr->data;
}
, , .
, node node node ..... ? node, .
?