[Rcpp-devel] Creating a std::vector of objects with const members

Douglas Bates bates at stat.wisc.edu
Fri Oct 7 14:25:53 CEST 2011


On Thu, Oct 6, 2011 at 6:23 PM, Darren Cook <darren at dcook.org> wrote:
>> It is not a big deal in this case but I am wondering how to define the
>> population protocol using something like std::vector so that
>> elementary protocols can be added or removed but each elementary
>> protocol is read-only.
>> ... like declaring the vector as std::vector<const ElemProt> but
>> that doesn't seem to work in the push_back method for the vector.
>
> That should work (or I've misunderstood the problem).
> "std::vector<const ElemProt>" means pass-by-value. Could that have
> caused trouble? (E.g. if you didn't have a copy constructor defined.)
>
> If so, std::vector<const ElemProt*> might work? (Or better is vector of
> smart pointers, so you don't have to worry about memory management.)
> Or, define a copy constructor.

Thanks for the suggestion, Darren.  One of my objectives is not to
copy the objects.  I have, in effect, created a copy constructor, to
get around the problem of const'ness.  I will look at your suggestion
of a vector of smart pointers.


More information about the Rcpp-devel mailing list