Why pass by reference for copy constructor?

I was unable to understand this part, it was confusing.

hello @S_yajur

A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass an argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.

I am unable to understand, why is there a non-terminating chain of calls?
Can u please explain in reference to this example:

class car
{
Car(Car x) // call by value
{
//body
}
}

main()
{
Car d;

Car d(e);   // calling copy constructor

}

here e will be passed as a parameter to this ->

copy constructor right?

yes sir, that is a copy constructor. but i still dont get it

so that means first
e will be copied to x right?

If u passed by value to copy constructer,it will call itself,to copy the actual parameter to formal parameter,it will call a endless chain of call to copy constucter,this process would go on untill the system runs out of memory

So you are saying for copying from e to x, again copy constructor would be called, but i thought copy constructor is invoked by
Car e(x); or Car x=e;

without these statements how it is calling the copy constructor again?

when e will be passed to copy constructor then first e will be copied to x.
and for copying e to x again copy constructor will be called.
and this will happen again n again

1 Like

so is it like when e is copying to x (for 1st time out of n times), it will call copy constructor bcos of which compiler will try to copy e to x(for 2nd time) and call copy constr and again compiler will try to copy e to x(for 3rd time)…and it goes on for n times.

Am i correct sir?

yeah …

Thank you sir for explaining

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.