I've got a container class that has its implementation hidden through the Pimpl idiom.
The problem is: how to expose a typedef defined within the implementation class onto the public class? Also, I want to make sure that the implementation details are not exposed within the typedef (we should not see it's a vector).
The code below complains during compilation saying that MyContainer::Impl
is an incomplete type. I just can't seem to find any workaround.
I've stumbled upon a similar question on How can I expose iterators without exposing the container used? pointing to an article about type erasures, but I'm not sure if I can apply all this reading to what I need?
I'm using C++11.
class MyContainer
{
private:
class Impl;
std::unique_ptr<Impl> m_d;
public:
typedef Impl::Iterator Iterator;
//typedef std::vector<int>::iterator Iterator; //< No!
MyContainer() = default;
~MyContainer() = default;
Iterator begin();
};
class MyContainer::Impl
{
public:
typedef std::vector<int>::iterator Iterator;
Impl() = default;
~Impl() = default;
std::vector<int> items;
};
MyContainer::Iterator MyContainer::begin()
{
return m_d->items.begin();
}