Hello Darren,<br><br>Thank you for your answer. It becomes much clearer to me now.<br>In particular, I really appreciate your "P.S.". Quite useful for beginners like me.<br><br>Best,<br>Zhongyi<br><br><div class="gmail_quote">
On Wed, Aug 31, 2011 at 1:40 AM, Darren Cook <span dir="ltr"><<a href="mailto:darren@dcook.org">darren@dcook.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">> NumericVector x = as<NumericVector>(x_); ( Btw, this is exactly the same as<br>
> NumericVector x(x_), both resulting in a shallow copy, isn't it? ) produces<br>
> a shallow copy, while<br>
> vector<double> x = as< vector<double> >(x_); produces a deep copy.<br>
><br>
> Is it because for NumvericVector the as() function returns the address of<br>
> x_? But why does it work differently for vector<double>?<br>
<br>
</div>Hello Zhongyi,<br>
I think if you put the namespaces on, explicitly, it becomes clearer:<br>
<br>
Rcpp::NumericVector x = as<Rcpp::NumericVector>(x_);<br>
std::vector<double> x = as< std::vector<double> >(x_);<br>
<br>
(I'm assuming x_ is the parameter being passed in from R.)<br>
<br>
x_ is effectively a pointer to a block of memory owned by R.<br>
Rcpp classes are just wrappers around that same pointer. Hence a shallow<br>
copy is possible.<br>
<br>
std::vector is a block of memory allocated and owned by your C++ code,<br>
so the bytes need to be physically copied over.<br>
<br>
Darren<br>
<br>
P.S. You'll see a lot of C++ code that keeps the std:: namespace<br>
definition in, rather than a using namespace std; at the top of the file.<br>
It adds 5 characters each time, but on the other hand it *only* adds 5<br>
characters each time.<br>
<br>
I don't know who invented this convention, or all the reasons it is<br>
good, but I personally do it that way, without regret.<br>
<br>
<br>
<br>
--<br>
Darren Cook, Software Researcher/Developer<br>
<br>
<a href="http://dcook.org/work/" target="_blank">http://dcook.org/work/</a> (About me and my work)<br>
<a href="http://dcook.org/blogs.html" target="_blank">http://dcook.org/blogs.html</a> (My blogs and articles)<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div><br>