Q. In terms of Memory Management.
Copy constructor for a class A has the form A(const A& a)
Why is the parameter passed by reference?
Explain why we cannot define a constructor of the form A(A a)
Answer.
Three reasons why we do so:
1. Constant Reference when passing an object as parameter is MORE efficient
2. If we pass A(A a) without const modifier, the object that is not supposed to be changed could possibly be changed by mistake
3. We do so so that we can make Deep Copy
Those three reasons are correct?
Please let me know.
Thanks,
1. That's true.
2. You're getting a copy, so it doesn't matter if you modify the object. True, your copy constructor shouldn't be modifying the rhs, but as long as you causing any side effects (you shouldn't) it's not technically wrong.
3. You can make a deep copy with without needing a reference...but see the following notes.
It is made a const reference to allow r-value/temporaries to be passed. If it were A(A a) then the copy constructor would need to be called to create that parameter's instance from the instance you are passing, resulting in it infinitely calling itself.