at timestamp 6:30 bhiya is saying that there is a cycle . What kind of cycle is this and how it is there i could not get. Please explain
Copy Consturctor
What is the difference between
int func(int x)
and
int func(int &x)
In the first function a copy of x is created for local use in the function, whereas in second function a memory reference of variable x is send to function and the function accesses the memory directly.
Now lets try to make a copy constructor:
myClass(myClass a)
In this function, just like we previously established a copy of object ‘a’ will be passed to the copy constructor, but wait, to make a copy of ‘a’, program needs a copy constructor. So again a copy constructor will be called and this will lead to a cycle.
Lets see a roundabout
myClass(myClass &a)
Here the function will be given a memory reference of ‘a’ which obviously exists in the memory and its members will be accessible for initializing members of the new (copy) object.
So If an object is passed as value to the Copy Constructor then its copy constructor would call itself, to copy the actual parameter to the formal parameter. Thus an endless chain of call to the copy constructor will be initiated. This process would go on until the system run out of memory.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.