Generic collection class in C++

B

Bit Byte

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 ?
 
M

mlimber

Bit said:
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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top