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. Advertising

  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. Advertising

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. Purush
    Replies:
    4
    Views:
    1,662
    Purush Rudrakshala
    Apr 13, 2005
  2. Murat Tasan
    Replies:
    1
    Views:
    8,050
    Chaitanya
    Feb 3, 2009
  3. Replies:
    2
    Views:
    437
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    978
    Øyvind Isaksen
    May 18, 2007
  5. minlearn
    Replies:
    2
    Views:
    456
    red floyd
    Mar 13, 2009
Loading...

Share This Page