1

I have a project in which I have to make a heterogeneous list of n containers, where n will be given. The containers can be a list, queue or stack and each of them must implement a bool member(T const& x) method, which checks if an object is present in the container. I also have to make an iterator for the heterogeneous list which can go through each element of all the containers(all the elements in the different containers will be of the same type).

My idea is to have an interface containing the member method and 3 new classes each of them inheriting from the interface and the stl containers ( I know it's a bad idea, but I don't want to write my own list, queue and stack classes). As for the the heterogeneous list I thought of having a class with a data member which is a list or vector of the interface class.

The things I don't know how to do are: how do I inherit exactly from an stl container for a generic type T. Don't know if something like this can work:

template <typename T>
class MyList : public list<T>, CommonInterface

Also the biggest problem I have is how to make the iterator for the heterogeneous list, so that it can traverse thought all elements of the containers. My idea is to transfer all the elements to one vector object, and use it's iterator (a kind of wrapping I guess).

Any ideas and solutions on this problem are welcomed. If there is something unclear about the question, I'll try to clarify it as soon as possible.

DVK
  • 126,886
  • 32
  • 213
  • 327
pesho
  • 111
  • 1
  • 9
  • This is a bad idea - the STL containers do not have virtual destructors, so they won't be cleaned up properly, if you do as you intend to. See http://stackoverflow.com/questions/2034916/is-it-okay-to-inherit-implementation-from-stl-containers-rather-than-delegate – Jens Munk Dec 27 '14 at 22:24
  • And you already have the iterators, which support some heterogeneousity – Jens Munk Dec 27 '14 at 22:26
  • if you decide to inherit for stl containers which is a bad idea as precised in the last comments, once should inherit in private not in public as preciser in the effective stl book of scott meyers. – Mohamed Ali Dec 27 '14 at 22:30
  • Don't think that the lack of virtual destructors, will be such a problem because it's a school project and I won't be creating that many containers. Also can you clarify on the iterators part, because I have no idea how to do the specific iterator that is wanted in question. Thanks :) – pesho Dec 27 '14 at 22:31
  • @MohamedAli why should I use private inheritance, that way I will lose all the public methods of the container class and I don't want to write public ones just to call them. – pesho Dec 27 '14 at 22:33
  • "checks if an object is present in the container" is not an operation commonly expected of a queue or a stack. Nor is the ability to iterate over all elements. The assignment doesn't make much sense to me. In particular, `std::stack` and `std::queue` don't provide iterator access (though they expose the underlying container as a protected member, so your derived class can iterate over that, if it's so inclined; at that point, there won't be much of the stack or queue behavior left). – Igor Tandetnik Dec 27 '14 at 23:44
  • @pesho Because private inheritance prevents _implicit_ conversions from `MyList` to `list` which _could_ be problematic. You also don't have to write forwarding functions in your derived class, a `using` statement can be used to pull those functions into it. it doesn't matter if this is a school project or not, doing things as correctly as possible should always be a **goal**. – Captain Obvlious Dec 27 '14 at 23:46

1 Answers1

0

This is a lot more complex than you think. The main reason is that C++ is statically typed and therefore not meant for this approach. But one solution is to use a collection of static containers and an unordered_map to be able to select the right container when you are traversing a particular typed container.

See this repo and example https://github.com/hosseinmoein/DataFrame

hmoein
  • 309
  • 2
  • 7