I don't understand how Copy Constructor will become recursive if we define the copy constructor with call by value concept. We cannot create like that because compile would not allow to create it.
Can anybody tell me Why we should pass by reference for the copy constructor??
} --> This is the correct behaviour. How this would be recursive if we remove the reference (&) in the copy constructor.
Appreciate your reply.
Thanks in advance,
The copy constructor is a concept to enable you to do call-by-value for a custom class. So defining a CC that takes a parameter by value would obviously call itself.
You are right that most (probably all) compilers would complain if you tried to create a CC with Call by value parameter, so you are safe here. There is another good reason to have a call by reference parameter:- efficiency. A reference is really nothing but a "pointer to an EXISTING object" so at runtime there won't be a copy created. Saves time and memory.
For an in depth discussion of CC see Volume 1 of "Thinking in C++" by Bruce Eckel. You can download the whole book as HTML from Bruce's web-site.
Hope that helps?
When you pass an object by value to a function, the callee gets a *copy* of the original object. This is hardly surprising because the same thing happens for example when you pass int by value. The major difference however between pasisng int and a class object is that when you pass the latter by value, the copy is created by invoking a copy-constructor. I other words, in order to an object x to a copy constructor of class X, the compiler must first invoke a copy constructor to create the argument for the copy constructor. And how would that be accomplished? You guessed: by invoking the copy-constructor for the second copy constructor, repeat ad lib.
Because of this, C++ requires that copy constructors shall not take arguments by value. I believe that every decent compiler these days rejects such code anyway. However, now you can see that this restriction makes a lot of sense...
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center