how to write a class that stores things in arbitrary stl container

Discussion in 'C++' started by 3doutpost, Dec 1, 2004.

  1. 3doutpost

    3doutpost Guest

    is there a way to write a class that will be used to store a list of
    things so that you can pass in the STL container type and it will be
    filled with the right thing - something like (if it were legal) ...

    std::vector < std::string > aList;

    funkyList* theList = new funkyList < aList >;

    theList->populate ();

    std::vector < std::string >::iterator iter = theList->getIterator ();

    .... hmmm.. as i'm writing that i realzie it's not making sense...
    what i'm trying to do is allow consumers of the class to use any
    storage they like and not enforce a std::vector, std::list etc...

    maybe i need to just return my own iterator to the list and allow the
    class consumer to do with each element what it wants rather than
    returning the whole list.

    comments appreciated.
     
    3doutpost, Dec 1, 2004
    #1
    1. Advertising

  2. 3doutpost

    Xenos Guest

    "3doutpost" <> wrote in message
    news:...
    > what i'm trying to do is allow consumers of the class to use any
    > storage they like and not enforce a std::vector, std::list etc...
    >

    Take a look at how std::queue is declared.
     
    Xenos, Dec 1, 2004
    #2
    1. Advertising

  3. 3doutpost wrote:

    > is there a way to write a class that will be used to store a list of
    > things so that you can pass in the STL container type and it will be
    > filled with the right thing - something like (if it were legal) ...
    >
    > std::vector < std::string > aList;
    >
    > funkyList* theList = new funkyList < aList >;
    >
    > theList->populate ();
    >
    > std::vector < std::string >::iterator iter = theList->getIterator ();
    >
    > ... hmmm.. as i'm writing that i realzie it's not making sense...
    > what i'm trying to do is allow consumers of the class to use any
    > storage they like and not enforce a std::vector, std::list etc...
    >
    > maybe i need to just return my own iterator to the list and allow the
    > class consumer to do with each element what it wants rather than
    > returning the whole list.
    >
    > comments appreciated.


    template<template<class> class Container>
    struct List {
    Container<std::string> c;
    };

    Daniel
     
    Daniel Mitchell, Dec 5, 2004
    #3
    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. Maitre Bart
    Replies:
    2
    Views:
    549
    Maitre Bart
    Feb 11, 2004
  2. Honestmath
    Replies:
    5
    Views:
    590
    Honestmath
    Dec 13, 2004
  3. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    626
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
  4. Replies:
    4
    Views:
    831
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    482
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page