Why copy constructor argument should be const in C
Why copy constructor argument should be const in C
Before Knowing that you have to know one more concept and that is –
http://www.geeksforgeeks.org/copy-constructor-argument-const/
X fun()
{
return X();
}
int main()
{
func(fun()); //Statement will generate error. Read more below
return 0;
}
Note: - Here Call to fun() function creating and returning the temporary object of class X as this is the
case of call by value.
So same way If we write copy constructor there also we always mention const
for reference parameter like this -
Now assume that the code for copy constructor is written like this –
Then from first instance it looks fine but there will be a compilation error in the highlighted line. Why
because call to function fun() here is returning a temporary object, and that temporary object you are
assigning to t2, and this is not assignment operation instead its creation of object from an existing object
[i.e. Creation of object t with fun() returned temporary object], so in this case copy constructor will get
called , where you can see copy constructor is taking nonconst reference as a parameter, which I
mentioned above is not allowed in c++ so it cause compilation error.
If we add const in copy constructor, the program works fine. Or if we change the highlighted line to following
two lines,
Test t2;
t2 = fun();
Then also the program works fine, because in this case copy constructor does not get called, instead
assignment operator gets called.
So the essence is it doesn’t make sense to modify compiler created temporary objects as they can die any
moment
class Test
{
public:
Test(Test &t)
{
cout << "Copy Constructor Called\n";
}
Test fun()
{
cout << "fun() Called\n";
Test t;
return t;
}
int main()
{
Test t1;
Test t2 = fun();
return 0;
}
Answer 1 Passing objects by value automatically creates a temporary copy of the object on the call
stack. So even if it were possible to pass by value in a copy constructor, the call would not only allocate
memory for the copy, it would call the copy constructor to initialise that memory, causing the copy
constructor to iterate over and over until there was no more memory left in the call stack. Ultimately,
your program crashes without ever having made a copy.
Answer 2 Because if it's not by reference, it’s by value. To do that you make a copy, and to do that you
call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor,
and so on...(You would have infinite recursion because "to make a copy, you need to make a copy".)
Answer 3 copy constructor not only accepts objects by reference but by constant reference. This is
because the copy constructor is only interested in copying the object's members, not in changing them.
As a general rule, if an object can be passed to any function by constant reference then that is the way
to go. If it is a non-constant reference, then the assumption is the object's immutable members will be
altered by the function in some way. If you do not want any changes reflected in your object, then you
must copy the object first, which is essentially what happens when you pass the object by value.