TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why should the copy constructor accept its parameter by reference in C++?

1 pointsby shivajikobardanover 2 years ago
<p><pre><code> #include &lt;iostream&gt; #include &lt;stdlib.h&gt; using namespace std; class Complex { private: int real; int img; public: Complex() { real = img = 0; } Complex(int r, int i) { real = r; img = i; } Complex(Complex &amp;c) { real = c.real; img = c.img; } void putComplex() { cout &lt;&lt; real &lt;&lt; &quot;+i&quot; &lt;&lt; img&lt;&lt;endl; } }; int main() { system(&quot;cls&quot;); Complex c1; Complex c2(3, 5); Complex c3(c2); c1.putComplex(); c2.putComplex(); c3.putComplex(); return 0; } </code></pre> I don&#x27;t understand how copy constructor works. 1) What happens when complex c3(c2) is called? Step by step? obviously, in short real gets c2.real and img gets c2.img, I&#x27;m asking for in depth explanation of behind the scenes.<p>2) Step by step what will happen if I remove the &quot;&amp;&quot; from &quot;c&quot; in copy constructor definition? I&#x27;ve heard that it goes to infinite recursion, but I don&#x27;t understand how.<p>I&#x27;ll be happy with any references to read or answers. https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;2685854&#x2F;why-should-the-copy-constructor-accept-its-parameter-by-reference-in-c I&#x27;ve already read this answer and failed to understand from there.

2 comments

NaNDudeover 2 years ago
the most important point to keep in mind is the default pass by value context.<p>without defining a copy constructor, the compiler will do the copy himself, the point is : there is no copy constructor to call, so you have :<p>1) first the new Complex c3 is allocated,<p>2) there is no copy constructor to call<p>3) the compiler do the job, the memory used by c2 is copied on c3.<p>end of the story, no parameter value.<p>when adding a legal copy constructor (pass by reference), you may consider the reference like some sort of pointer. this is false, but help to understand the memory behavior of a copy constructor. then you have something like :<p>1) first the new Complex c3 is allocated<p>2) the &quot;memory&quot; for c, the reference&#x2F;pointer to c2, is allocated.<p>3) the _address_ of c2 is copied in c.<p>4) the c3 constructor start with the reference&#x2F;pointer&#x2F;address of c2 as parameter.<p>end of the story, normal &quot;pass by value&quot; of an address.<p>finaly, if a copy constructor _without_ reference&#x2F;pointer was legal, you will have:<p>1) first the new Complex c3 is allocated,<p>2) another new Complex, the c3 copy constructor parameter value c, is allocated.<p>3) instead of directly copying c2 memory to c, there is the c copy constructor to call (with a copy of c2 as parameter value)<p>4) another new Complex c_bis, the c copy constructor parameter value (c), is allocated.<p>5) instead of directly copying c2 memory to c_bis there is the c_bis copy constructor to call (with a copy of c2 as parameter value)<p>6) another new Complex c_bis_bis is allocated ... etc.<p>the problem is that a copy constructor is _implicitly_ called to create a copy, which transform a pass by value semantic in an infinite loop of allocations on the stack.<p>hope this help.
评论 #34638034 未加载
shivajikobardanover 2 years ago
Correct me if I&#x27;m wrong: In int main, &quot;complex c3(c2)&quot; creates a copy of c2 and thus passes c2 to copy constructor.<p>In copy constructor, &quot;complex(complex c)&quot; copy constructor takes c=c2.<p>So what&#x27;s the issue with this?
评论 #34662459 未加载
评论 #34662639 未加载