Hello Darren,<br><br>Thank you for your answer. It becomes much clearer to me now.<br>In particular, I really appreciate your &quot;P.S.&quot;. 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">&lt;<a href="mailto:darren@dcook.org">darren@dcook.org</a>&gt;</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">&gt; NumericVector x = as&lt;NumericVector&gt;(x_); ( Btw, this is exactly the same as<br>
&gt; NumericVector x(x_), both resulting in a shallow copy, isn&#39;t it? ) produces<br>
&gt; a shallow copy, while<br>
&gt; vector&lt;double&gt; x = as&lt; vector&lt;double&gt; &gt;(x_); produces a deep copy.<br>
&gt;<br>
&gt; Is it because for NumvericVector the as() function returns the address of<br>
&gt; x_? But why does it work differently for vector&lt;double&gt;?<br>
<br>
</div>Hello Zhongyi,<br>
I think if you put the namespaces on, explicitly, it becomes clearer:<br>
<br>
Rcpp::NumericVector x = as&lt;Rcpp::NumericVector&gt;(x_);<br>
std::vector&lt;double&gt; x = as&lt; std::vector&lt;double&gt; &gt;(x_);<br>
<br>
(I&#39;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&#39;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&#39;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>