vector<Foo*>& getVectorOfFoo();
I want to provide a list of my Foo objects to others. Is this the best way to do it? I am returning a reference here and not copying correct?
The caller could (accidentally) modify this list right? Is there a way to avoid this possibility? Could I return const vector ? But they could always modify the Foo objects as well and not much I can do there. 10-20 different people will be writing code that use this list of Foo.
First don’t return a list of pointers.
That makes it doubly unclear about the allowed actions.
Boost has a solution (as usual).
Return a pointer container. This exposes the pointers as normal members.
Now the user can not alter the returned vector.
Example: