Generic collection class in C++

Discussion in 'C++' started by Bit Byte, Nov 7, 2006.

  1. Bit Byte

    Bit Byte Guest

    I was thinking of writing a generic container (i.e. collection) class in
    C++. It would have iterators etc to allow traversing and acessing the
    dat items.

    The only problem arose when I tried to implement it, as I realised that
    most of the STL containers actually required a type at compile time. Any
    ideas on how to proceed with this? (or am I re-inveting the wheel here?)

    Snippet of class:

    template <class T>
    class Collection
    {
    <snip>
    </snip>
    private:
    vector<T> m_items ; //just an illustration, wont work. I know
    };

    I don't want to use template specialization because then the class is
    not generic enough. Also, I would like to be able to store pointers in
    such a collection, using value semantics, if possible - anyone knows how
    to do this ?
     
    Bit Byte, Nov 7, 2006
    #1
    1. Advertisements

  2. Bit Byte

    mlimber Guest

    Bit Byte wrote:
    > I was thinking of writing a generic container (i.e. collection) class in
    > C++. It would have iterators etc to allow traversing and acessing the
    > dat items.
    >
    > The only problem arose when I tried to implement it, as I realised that
    > most of the STL containers actually required a type at compile time. Any
    > ideas on how to proceed with this? (or am I re-inveting the wheel here?)
    >
    > Snippet of class:
    >
    > template <class T>
    > class Collection
    > {
    > <snip>
    > </snip>
    > private:
    > vector<T> m_items ; //just an illustration, wont work. I know
    > };
    >
    > I don't want to use template specialization because then the class is
    > not generic enough. Also, I would like to be able to store pointers in
    > such a collection, using value semantics, if possible - anyone knows how
    > to do this ?


    Use Boost.Any and/or a smart pointer. See
    <http://boost.org/doc/html/any.html>.

    Cheers! --M
     
    mlimber, Nov 7, 2006
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Baggs
    Replies:
    5
    Views:
    43,933
    Steven Baggs
    Oct 18, 2005
  2. Purush
    Replies:
    4
    Views:
    2,621
    Purush Rudrakshala
    Apr 13, 2005
  3. Murat Tasan
    Replies:
    1
    Views:
    8,447
    Chaitanya
    Feb 3, 2009
  4. E11
    Replies:
    1
    Views:
    5,786
    Thomas Weidenfeller
    Oct 12, 2005
  5. Replies:
    3
    Views:
    1,765
    Tim B
    Oct 18, 2005
  6. Replies:
    2
    Views:
    648
  7. Øyvind Isaksen
    Replies:
    1
    Views:
    1,286
    Øyvind Isaksen
    May 18, 2007
  8. minlearn
    Replies:
    2
    Views:
    665
    red floyd
    Mar 13, 2009
Loading...