I have a class like the one below:
class Foo {
private:
std::map<std::string, Bar*> bars_by_name;
std::map<std::string, Baz*> bazs_by_name;
};
Now I’d like to allow the user to access both collections but hide the implementation detail that I’m storing the objects into std::maps. Instead, I’d like to have member functions that return e.g. const iterators for the collections and possibly even a custom iterator that returns objects from both collections, since Bar and Baz belong to the same class hierarchy. Considering style, what would be the proper way to do this in C++? In Java, I’d probably set the method’s return type to Iterable or wrap the collection into an unmodifiableCollection.
You could pass the collection’s iterators off as your own.
This saves you the work of implementing your own iterator, yet leaves you the flexibility of changing your container types later and only requiring callers to recompile.
It doesn’t solve the problem of iterating them both together.