Thread: Void* and Template for Linked List

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Void* and Template for Linked List

    Hi! guys! I have a question about generic linked list.
    This is an example using C.

    Code:
    typedef struct list{
    	void *info;
    	struct list *next;
    } List;
    but now I have to translate it to C++, but I must use template instead of void*.


    Code:
    template <class T>
    class Node{
    	public:
    	T info;
    	Node<T>* next;
    	Node(T m, Node<T> *p = NULL) {info = m; prox = p;}
    };
    
    template <class T>
    class List{
    	Node<T> *first;
    	
    	public:
    	List(void){ first = NULL; }
            void insert(T m);
    	void print();
    };

    I figured out that I can only get a list with one type node and not a list with different types of nodes.

    Code:
    int main(){
              // One type nodes
            List<int> L;
            List<float> F;
            List<string> S;
    }
    How can I use templates with generic data nodes?

    example:

    *FirstPointer -> int -> float -> string -> int -> char -> NULL

    thx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How can I use templates with generic data nodes?
    Perhaps by using a List<boost::any>, but I am not familiar with boost::any myself.

    Why do you want a linked list with nodes of varying unrelated types in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Actually I was trying to make a list of different basic types and then to make one with generic classes.

    Now I got the idea.

    thx

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is possible to do it, but... Perhaps boost is easier (I don't know myself). Otherwise you will need to use polymorphism and probably a variable to remember the type in the linked list, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM