Quick A: By exposing wrapped iterators in your interface.
From Programmers StackExchange, StackOverflow's sister site:
Allow iteration of an internal vector without leaking the implementation
I have a class that represents a list of people.
class AddressBook { public: AddressBook(); private: std::vector<People> people; }I want to allow clients to iterate over the vector of people. The first thought I had was simply:std::vector<People> & getPeople { return people; }However, I do not want to leak the implementation details to the client. I may want to maintain certain invariants when the vector is modified, and I lose control over these invariants when I leak the implementation.
What's the best way to allow iteration without leaking the internals?
Add a Comment
Comments are closed.