0% found this document useful (0 votes)
16 views

Why copy constructor argument should be const in C

In C++, copy constructor arguments should be const to prevent binding temporary objects to non-const references, which can lead to uninitialized references if the temporary object is deleted. Passing by reference avoids unnecessary copying and potential infinite recursion that would occur if passing by value. The copy constructor is designed to copy an object's members without modifying them, hence it should accept constant references.

Uploaded by

Anoop Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Why copy constructor argument should be const in C

In C++, copy constructor arguments should be const to prevent binding temporary objects to non-const references, which can lead to uninitialized references if the temporary object is deleted. Passing by reference avoids unnecessary copying and potential infinite recursion that would occur if passing by value. The copy constructor is designed to copy an object's members without modifying them, hence it should accept constant references.

Uploaded by

Anoop Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) 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/

“Compiler created temporary objects cannot be bound to non-const


references”

Temporaries can't be bound to non-const


references
class X
{
int i;
};

X fun()
{
return X();
}

void func(X &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.

func(fun()); //Statement will generate error. Because Here fun() returns


temporary object and that same temporary object we are sending to func(),
which takes nonconst reference as parameter. So we are trying to bind that
temporary object with the nonconst reference parameter, which is not allowed
in C++ because who knows if those temporary objects gets lost/deleted then
these reference type parameter will be left uninitialized. And we know that
reference must be initialized always.

So same way If we write copy constructor there also we always mention const
for reference parameter like this -

Test (const Test &t)


{
cout << "Copy Constructor Called\n";
}

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() { /* Initialize data members */ }


};

Test fun()
{
cout << "fun() Called\n";
Test t;
return t;
}

int main()
{
Test t1;
Test t2 = fun();
return 0;
}

2) Why copy constructor argument should be of reference type


in C++?
http://wiki.answers.com/Q/Why_copy_constructor_will_take_reference_object_as_argument#slide2

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.

You might also like