Andre Poenitz wrote: > On Tue, Nov 04, 2003 at 02:00:21PM +0000, Angus Leeming wrote: >> Andre Poenitz wrote: >> >> > On Tue, Nov 04, 2003 at 12:19:25PM +0000, Angus Leeming wrote: >> >> I have a container that currently allows the user to alter the >> >> Node container. I want to allow the user to be able to alter the >> >> Node contents but to not be able to insert, erase Nodes from the >> >> vector. >> >> >> >> What's the best way to proceed? >> >> > /// The element list is immutable. >> > //std::vector<Element> const & elements() const { return >> > elements_; } Element const & element(size_t i) const { >> > return elements_[i]; } Element & element(size_t i) { >> > return elements_[i]; } >> >> Actually, it's perfectly safe to provide iterators isn't it, so >> long as I don't provide access to the storage container itself? > > Well... this exposes an implementation detail (type of container) to > the outer world. Moreover, people are tempted to use ++ on > iterators, whereas not too many poeple would use (&element(3) + 1)
I don't mind the ++bit. I'm happy with user code: typedef ElementList<quadratic::Element>::const_element_iterator const_element_iterator; const_element_iterator elem_it = elist.begin_elements(); const_element_iterator const elem_end = elist.end_elements(); for (; elem_it != elem_end; ++elem_it) do_something(*elem_it); typedef ElementList<quadratic::Element>::node_iterator node_iterator; node_iterator node_it = elist.begin_nodes(); node_iterator const node_end = elist.end_nodes(); for (; node_it != node_end; ++node_it) node_it->set_coord(makeCoord(1.0, 2.0, 3.0)); The important bit is that the user is unable to alter the container itself (erase, insert). > So _I_'d prefer the accessor functions. Yes, I can see why. However, LyX is trying to use iterators more, not less, and you are driving that effort. Thay can't be all that bad ;-) -- Angus