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

3

3doutpost

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

Xenos

3doutpost said:
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.
 
D

Daniel Mitchell

3doutpost said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top