Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>A deep copy is then: [for loop]</p> </blockquote> <p>You make the client clone the vector explicitly. I'm not sure if this answers your question, but I would suggest a vector of smart pointers so the cloning happens automatically.</p> <pre><code>std::vector&lt;cloning_pointer&lt;Base&gt; &gt; vec; vec.push_back(cloning_pointer&lt;Base&gt;(new Derived())); // objects are automatically cloned: std::vector&lt;cloning_pointer&lt;Base&gt; &gt; vec2 = vec; </code></pre> <p>Of course, you don't want these implicit copies to happen when resizing a vector or something, so you need to be able to distinguish copies from moves. Here is my C++0x toy implementation of <code>cloning_pointer</code> which you might have to adjust to your needs.</p> <pre><code>#include &lt;algorithm&gt; template&lt;class T&gt; class cloning_pointer { T* p; public: explicit cloning_pointer(T* p) { this-&gt;p = p; } ~cloning_pointer() { delete p; } cloning_pointer(const cloning_pointer&amp; that) { p = that-&gt;clone(); } cloning_pointer(cloning_pointer&amp;&amp; that) { p = that.p; that.p = 0; } cloning_pointer&amp; operator=(const cloning_pointer&amp; that) { T* q = that-&gt;clone(); delete p; p = q; return *this; } cloning_pointer&amp; operator=(cloning_pointer&amp;&amp; that) { std::swap(p, that.p); return *this; } T* operator-&gt;() const { return p; } T&amp; operator*() const { return *p; } }; </code></pre> <p>Julien: <code>&amp;&amp;</code> is not a "ref of ref", it is an rvalue reference which only binds to modifiable rvalues. See this excellent (but sadly slightly outdated) <a href="http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx" rel="nofollow noreferrer">tutorial</a> and <a href="http://channel9.msdn.com/shows/Going+Deep/VC-10-Stephan-T-Lavavej-and-Damien-Watkins-Inside-STL/" rel="nofollow noreferrer">video</a> for an overview of rvalue references and how they work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload