<p>In C++, what alternatives do I have for exposing a collection, from the point of view of performance and data integrity?</p>

<p>My problem is that I want to return an internal list of data to the caller, but I don't want to generate a copy. Thant leaves me with either returning a reference to the list, or a pointer to the list. However, I'm not crazy about letting the caller change the data, I just want to let it read the data.  </p>

<ul>
<li>Do I have to choose between performance and data integrity?  </li>
<li>If so, is in general better to go one way or is it particular to the case?  </li>
<li>Are there other alternatives?</li>
</ul>

Maybe something like this?
```
```
const std::vector<mydata>& getData()
{
  return _myPrivateData;
}
```
```
The benefit here is that it's very, very simple, and as safe as you getin C++.  You can cast this, like RobQ suggests, but there's nothing you can do that would prevent someone from that if you're not copying.  Here, you would have to use
```
const_cast
```
, which is pretty easy to spot if you're looking for it.
Iterators, alternatively, might get you pretty much the same thing, but it's more complicated.  The only added benefit of using iterators here (that I can think of) is that you can have better encapsulation.