Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>General notes</strong> (before you edited your question):</p> <p>Just calling the copy assignment operator in a copy constructor is not a general solution. They mean different things (I mean, why should they be there if they meant the same).</p> <ul> <li><p>The <strong>assignment operator</strong> is called when an (existing!) object is assigned a new value. If this object is of the same type, we call this also the <em>copy assignment</em>, since a typical implementation simply copies the contents (but might share some referenced things, like for example shared pointers or PIMPL classes with shared data). A copy assignment operator is implemented by the compiler automatically unless you provide one. It copies each member using the assignment operators of their types (primitive members are also copied).</p></li> <li><p>The <strong>copy constructor</strong> is called when a (not yet existing!) obejct is assigned an initial value of the same type, i.e. it should be initialized with the copy of an existing object. Again, if you don't provide a copy constructor, the compiler generates one for you, again just copying the members using the copy constructor.</p></li> </ul> <p>If you call the assignment operator from within the copy constructor, this means that the generated program performs the following steps when copy-initializing a new object:</p> <ul> <li>(Unless you use member-initializer list:) It initializes non-primitive class members with default constructors. Primitive types are left uninitialized.</li> <li>Then, the assignment operator is called. If you didn't define one, it copies all members.</li> </ul> <p>So it <em>should</em> be fine in most cases, but there are a couple of cases in which this does <em>not</em> work, in particular if your class has members which <em>can't</em> be default-constructed or can't be assigned. If this is the case, and if they can still be copy-constructed (in contrast to copy-assigned), you have to initialize the members in the member-initialization list of the copy constructor manually.</p> <hr> <p><strong>EDIT</strong> (Since the question got edited): In your case, <code>data</code> is a primitive type (all pointers are considered primitive), so you have to initialize it properly in the copy constructor before calling your assignment operator. If you don't do so, the assignment will delete an uninitialized pointer. So the best you should to (to avoid code duplication; it could be more efficient if you did):</p> <pre><code>Array&lt;T, ROW, COL&gt;(const Array&lt;T, ROW, COL&gt; &amp;array) : data(0) // Now it is at least initialized, although inefficient { *this = array; } </code></pre> <p>The assignment operator will then try to delete a null-pointer, which is okay (it just does nothing), and then performs the actual copy. Consider the <code>data(0)</code> initialization just as a "default-constructed null <code>Array&lt;...&gt;</code>" object, just temporarily. (Maybe you already provide a null-object which does <em>not</em> allocate external memory?)</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