I have a class which has a std::vector of child control pointer. For obvious reasons, I do not want the user of the class to have direct access to the std::vector. All I would want is a way to give the caller the pointers. What would be a good OO way to do this? (this function will be called often)
Thanks
I usually do it something like the following:
Then you can just do something like:
No iterators to the outside world required. If you have ever have to expose something like this to a standard C API then it's very easy to expose.
Provide a function that returns a
const_iterator
to the vector. It is also useful to add one to return the iterator to the end of the vector.Iterators are a good, obvious way to do this. A visitor pattern is another way to give the client code the ability to operate on each element in the vector: in some ways it's even cleaner, exposing less to the user, and allowing the container more control, e.g.:
BUT
begin()
, incremented or not, as well as other operations likefind()
: this is a cleaner factoring of functionality.That would look something like: