Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To paraphrase our question:</p> <blockquote> <p>why do we have to write <code>Date(const Date &amp;)</code> instead of <code>Date(Date)</code>?</p> </blockquote> <p>I'm going to split this into two parts, the first answering why a copy constructor needs to take its argument per reference, the second why this needs to be a const reference. </p> <hr> <p>The reason a copy constructor needs to take its argument per reference is that, for a function that's taking an argument <em>per copy</em> <code>void f(T arg)</code>, when you call it <code>f(obj)</code>, <code>obj</code> is <strong><em>copied</em></strong> into <code>arg</code> <em>using <code>T</code>'s copy constructor</em>. So if you want to implement the copy constructor, you'd better not take the argument by copy, because this would call the copy constructor while invoking it, leading to an endless recursion. You can easily try this yourself: </p> <pre><code>struct tester { tester(tester) {std::cout &lt;&lt; "inside of erroneous copy ctor\n";} }; int main() { tester t1; std::cout &lt;&lt; "about to call erroneous copy ctor\n"; tester t2(t1); std::cout &lt;&lt; "done with call erroneous copy ctor\n"; return 0; } </code></pre> <p>That program should only ever write one line and then blow the stack.<br> <em>Note:</em> As Dennis points out in his comment, actually this program is not guaranteed to compile, so, depending on your compiler, you might not really be able to try it. </p> <p>Bottom line: <em>A copy constructor should take its argument <strong>by reference</strong>, because taking it per copy would require the copy constructor.</em> </p> <hr> <p>That leaves the question of <strong><em>why it is <code>const T&amp;</code> and not simply <code>T&amp;</code></em></strong>? In fact, there are two reasons for that.<br> The logical reason is that, when you invoke the copy constructor, you do not expect the object copied from to change. In C++, if you want to express that something is immutable, you use <code>const</code>. This tells users that they can safely pass their precious objects to your copy constructor, because it won't do anything with it except read from it. As a nice side effect, if you implement the copy constructor and accidentally try to write to the object, the compiler throws an error message at you, reminding you of the promise made to the caller.<br> The other reason is that you cannot bind temporary objects to non-<code>const</code> references, you can only bind them to <code>const</code> references. A temporary object is, for example, what a function might return: </p> <pre><code>struct tester { tester(tester&amp; rhs) {std::cout &lt;&lt; "inside of erroneous copy ctor\n";} }; tester void f() { tester t; return t; } </code></pre> <p>When <code>f()</code> is called, a <code>tester</code> object is created inside, and a copy of it is then returned to the caller, which might then put it into another copy: </p> <pre><code>tester my_t = f(); // won't compile </code></pre> <p>The problem is that <code>f()</code> returns a temporary object, and in order to call the copy constructor, this temporary would need to bind to the <code>rhs</code> argument of <code>tester</code>'s copy constructor, which is a non-<code>const</code> reference. But you cannot bind a temporary object to a non-<code>const</code> reference, so that code won't compile.<br> While you can work around this if you want (just don't copy the temporary, but bind it to a <code>const</code> reference instead, which extends the temporary's lifetime to the end of the reference's lifetime: <code>const tester&amp; my_t = f()</code>), people expect to be able to copy temporaries of your type. </p> <p>Bottom line: <em>A copy constructor should take its argument by <strong>const reference</strong>, because otherwise users might not be willing or able to use it.</em></p> <hr> <p>Here's one more fact: In the next C++ standard, you can overload functions for temporary objects, so-called <strong><em><code>rvalues</code></em></strong>. So you can have a special copy constructor that takes temporary objects overloading the "normal" copy constructor. If you have a compiler that already supports this new feature, you can try it out: </p> <pre><code>struct tester { tester(const tester&amp; rhs) { std::cout &lt;&lt; "common copy ctor\n"; } tester( tester&amp;&amp; rhs) { std::cout &lt;&lt; "copy ctor for rvalues\n"; } }; </code></pre> <p>When you use the above code to invoke our <code>f()</code></p> <pre><code>tester my_t = f(); </code></pre> <p>the new copy constructor for rvalues should be called when the temporary object returned by the call to <code>f()</code> is copied to <code>my_t</code> and the regular copy constructor <em>might</em> be called in order to copy the <code>t</code> object from inside of <code>f()</code> to the returned temporary. (Note: you might have to disable your compiler's optimization in order to see this, as the compiler is allowed to optimize away all the copying.)<br> So what can you with this? Well, when you copy an rvalue, you know that the object copied from is going to be destroyed after the call to the copy constructor, so the copy constructor taking an rvalue (<code>T&amp;&amp;</code>) could just <em>steal</em> the values from the argument instead of copying them. Since the object is going to be destroyed anyway, nobody is going to notice.<br> For some classes (for example, for string classes), <em>moving</em> the value from one object to another could be much cheaper than <em>copying</em> them. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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