Polymorphic Collections

closed account (1yR4jE8b)
Hello everyone,

I was hoping to gather some opinions. I'm writing a collections library (purely academic reasons), and I would like to gather some opinions about a design choice I need to make.

Basically, what I want to do is be able to choose the underlying container. One way I thought of doing it would be to have a large amount of abstract classes that you could apply to the containers something like this:

1
2
3
Queue<int>* q1 = new LinkedList<int>();
//OR
Queue<int>* q2 = new ArrayList<int>();


This would involve lots of a different abstract classes as well as a disturbing amount of multiple inheritance (which could cause some major problems).

Another option is to implement it using templates, where the underlying container is determined by template parameters.
1
2
3
Queue<int, LinkedList<int>  >* q1 = new Queue<int, LinkedList<int> >();
//OR
Queue<int, LinkedList<int> >* q2 = new Queue<int, LinkedList<int> >();


From my research, it seems the STL does it this way, so it seems like a logical approach. Then again, I'm not sure if I just want to rewrite the STL...and template compiler errors are a massive headache XD

Thoughts?

Last edited on
Create a base class called "Container" then provide some simple pure virtual methods on it (e.g. AddElement, CountElements etc). Develop a child class called QueueContainer, StackContainer etc that implement their STL counter-part. Once this has been done you can create an instance of Container* that will allow you to use any STL container underneath; through a standard API.
Topic archived. No new replies allowed.