If you want to store a const
reference to Foo
then use boost::cref()
and boost::reference_wrapper
as plain references are not assignable and cannot be stored in a vector
:
std::vector<boost::reference_wrapper<const Foo> > _storedContainers;
virtual void store(const Foo& container)
{
_storedContainers.push_back(boost::cref(container));
}
However, the elements in _storedContainers
have a potential to become dangling references if the objects passed to store()
are destructed while still required. Online demo at http://codepad.org/VOokOm6i.
Example (using equivalent c++11 versions of cref()
and reference_wrapper
) http://ideone.com/0vVv8w .