I have a collection, as an example, based of type std::vector<MyClass>. How can I create a collection wrapper class that exposes only a count() property and an index operator?
I am struggling on how to make collection classes based on templates and/or inheritance. My library needs full access to the STL container for add and deleting elements. My library client should only see to a couple of read-only methods. My target is Linux.
template<class T> //you can add the allocator template here too, if you need it.
class my_vector {
public:
//put here all the functions you want to expose, directly calling vectors appropriate function
//example:
my_vector(unsigned n, const T& value = T()) : vec(n,value) {}
T& operator[](unsigned i) { return vec[i]; }
private:
std::vector<T> vec;
};
This solution on a good complier makes no overhead.
You could solve the problem very similarly with private inheritance, I'm not sure which one is better, I would chose the one I made an example for.