collection of heterogeneous colletion

This is a discussion on collection of heterogeneous colletion within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm newbie in C++ and I've been trying to do what the title of this thread said but i ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Question collection of heterogeneous colletion

    Hi, i'm newbie in C++ and I've been trying to do what the title of this thread said but i don't get it :s, I'm trying to do a table something like that:

    | field1 | field2 | field3 | field5 |
    ---------------------------------------------------
    | (int) | (float) | (char) | (double) |
    | (int) | (float) | (char) | (double) |
    .
    .
    .
    | (int) | (float) | (char) | (double) |

    where the table isn't static i mean the number of fields are variable (field1, field2...fieldn) and the number of values of every field are either variable of fixed..

    To implement that I thought in a vector of vectors :s.. I made a class(named Fields) where a member is for its name and a vector for its values..
    And I made another class for contain those fields (named Table), which has, among other members, a vector of Field's..

    The problem is that I made the class Fields as template class in order to be able to have a vector of values of any type (as can see above in the table)..
    But in the Table class I cannot just declare something like ... "vector<Fields> fields" because I have an error, so I have to type: "vector< Fields<type > >" so I have to declare Table class as template class too, in order I to be able to put in it: "vector< Fields<T> > fields"... but the real problem is when I want to use them.. for explample, if I want to declare a new table I need to do something like: "Table<type> table;" but the problem is that I cannot put a type because the Table must contain diferent data types...

    So I hope you can understand me :s.. If so, I hope someone can help me.. Thanks in advanced
    Last edited by nmxnmx; 10-27-2005 at 11:32 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you can use a vector of vectors, but the innermost vector should probably be a union, not a template
    Code:
    struct udata
    {
       int type; // type of data storedhere
       union
       {
             char cdata;
             int    idata;
             double ddata;
       };
    };
    
    typedef vector<udata> COLUMNS;
    vector<COLUMNS> rows;

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Keep using your templates, just hide the implementation of Fields behind another class so that Fields is not templated but its inner implementation class is.

    I believe the implementation of Boost::any is an example of this. Of course, you might just be able to make Fields hold Boost::any objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. garbage collection enabled c++
    By manav in forum C++ Programming
    Replies: 42
    Last Post: 02-15-2008, 12:42 PM
  2. collection
    By AngKar in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:47 AM
  3. C++ Garbage Collection
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2005, 09:26 AM
  4. username collection
    By algi in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 01:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21