Search code examples
c++constantsstdlist

Exposing a std::list as read only


I have a class that contains, among other things, an std::list. I want to expose this list but only in such a way that the structure and the data it contains are read only, but still can be used with iterators.

The way I've got it 'working' atm is to return a copy of the list. This leave my class 'safe' but of course does nothing to stop the caller from modifying their copy of the list and not getting the right data.

Is there a better way?


Solution

  • Why not return a const std::list& instead?