Because otherwise you will pass the object to copy as an argument of copy constructor as pass by value which by definition creates a copy and so on... an infinite call chain....Do you see that-Prashant
Because otherwise you will pass the object to copy as an argument of copy constructor as pass by value which by definition creates a copy and so on... an infinite call chain....
well i dont feel thats the exact reason..., the reason stated by sneha is true if i ask why the argument of copy constructor is passed as a reference ...the reason for constant is that in copy constructor u never need to change the value of the passed object so to avoid accidental change it is passed as constant .....
const access specifier lets the constructor accept temporary objects that may be returned from another function. Temporary objects by default are constants and hence if const is not specified argument may not be accepted by the compiler. Not sure if all compilers does this.
Here we need to remember one thing that when ever an object is sent as a parameter as a pass value the constructor is called so if dont mention classname(const classname&) then the constructor will be calling itself
passing the objs as arguments by reference, allows NOT to call the zero argument constructor in cases 1. calling the copy const directly 2. when passing the objects to/frm funcs. 3. in case of assignment operator overloading but when objs are passed as const reference ,it prevents the accident change of the passed object as argument
Copy constructor is called when one object is used to initialize other object. Declaring this calls copy constructor during object to object initializing. Default bitwise copy is not done. The const pointer is reference to the object on the right side.