<p><pre><code> #include <iostream>
#include <stdlib.h>
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 &c)
{
real = c.real;
img = c.img;
}
void putComplex()
{
cout << real << "+i" << img<<endl;
}
};
int main()
{
system("cls");
Complex c1;
Complex c2(3, 5);
Complex c3(c2);
c1.putComplex();
c2.putComplex();
c3.putComplex();
return 0;
}
</code></pre>
I don'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'm asking for in depth explanation of behind the scenes.<p>2) Step by step what will happen if I remove the "&" from "c" in copy constructor definition? I've heard that it goes to infinite recursion, but I don't understand how.<p>I'll be happy with any references to read or answers.
https://stackoverflow.com/questions/2685854/why-should-the-copy-constructor-accept-its-parameter-by-reference-in-c
I've already read this answer and failed to understand from there.
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 "memory" for c, the reference/pointer to c2, is allocated.<p>3) the _address_ of c2 is copied in c.<p>4) the c3 constructor start with the reference/pointer/address of c2 as parameter.<p>end of the story, normal "pass by value" of an address.<p>finaly, if a copy constructor _without_ reference/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.
Correct me if I'm wrong:
In int main, "complex c3(c2)" creates a copy of c2 and thus passes c2 to copy constructor.<p>In copy constructor, "complex(complex c)" copy constructor takes c=c2.<p>So what's the issue with this?