Can a copy-constructor take a non-const parameter?
<p>I have this problem, there is a function <code>foo()</code> as follows,</p>

<pre><code>vector&lt;ClassA&gt; vec;

void foo()
{
    ClassA a;   //inside foo, a ClassA object will be created
    a._ptr = new char[10];

    vec.push_back(a);   //and this newly created ClassA object should be put into vec for later use
}
</code></pre>

<p>And AFAIK, <code>vec</code> will invoke <code>ClassA</code>'s copy-ctor to make a copy of the newly created object <code>a</code>, and here is the problem. If I define <code>ClassA</code>'s copy-ctor the usual way, </p>

<pre><code>ClassA::ClassA(const ClassA &amp;ra) : _ptr(0)
{
    _ptr = ra._ptr;
}
</code></pre>

<p>then object <code>a</code> and its copy (created by vec) will have pointers <code>_ptr</code> pointing to the same area, when <code>foo</code> finishes, <code>a</code> will call the destructor to release <code>_ptr</code>, then <code>a</code>'s copy in <code>vec</code> will be a dangling pointer, right? Due to this problem, I want to implement <code>ClassA</code>'s copy-ctor this way,</p>

<pre><code>ClassA::ClassA(ClassA &amp;ra) : _ptr(0) //take non-const reference as parameter
{
    std::swap(_ptr, a._ptr);
}
</code></pre>

<p>Is my implementation ok? Or any other way can help accomplish the job?</p>
