Thread: Passing data type

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Passing data type

    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;
    }
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    If you want to make a container like vector (Although yours looks more like a linked list), use templates.

    your class would look something like
    Code:
    template <typename T>
    class mycontainer
    {
        T data;
        mycontainer<T> *next;
    public:
        // Accessor functions
    };
    another advantage to using templates is that it doesn't matter what data types you use, the class treats them all the same.


    Edit : When creating a class like this (or any templated class), I strongly recommend you start out with a POD type such as an int. Templates always bring their own problems into the mix, so if you're creating a generic linked list such as std::list, make one which is specific to one type, and deal with any bugs which result from that first. When that is working, convert it to work with any type.
    Last edited by Bench82; 04-20-2006 at 05:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getc and int data type
    By Zeiro in forum C Programming
    Replies: 3
    Last Post: 08-13-2008, 10:25 AM
  2. return a random data type?
    By Niara in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2005, 12:58 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM