Suppose that I have an object that has a very large map. Consider now, another object that wishes to iterate through every item in the map owned by the first object. Lets name these two objects, ObjMap and ObjNoMap. It is tempting to write a ObjMap::getMap function that returns a pointer to the internal map object. However, this breaks encapsulation. Another option is to return the whole map by value, but then there is a very large hit in efficiency. Still another option would be to return a const_iterator. Consider this last option. If I return a const_iterator to the map from ObjMap am I still breaking encapsulation? On the one hand ObjNoMap will gain read access to an internal member of ObjMap, but at least ObjNoMap cannot mutate the internal member of ObjMap. Is there a viable solution that will allow me to keep with the object oriented methodology of encapsulation without taking a majore hit in efficiency?
Thank you for your time.