Question:
What is importance of const. pointer in copy constructor?
Answer:
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 Source: CoolInterview.com
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.... Source: CoolInterview.com
Answered by: Sneha | Date:
| Contact Sneha
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 ..... Source: CoolInterview.com
Answered by: harpreet walia | Date: 10/25/2007
| Contact harpreet walia
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. Source: CoolInterview.com
Answered by: Diya | Date: 1/3/2008
| Contact Diya
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 Source: CoolInterview.com
Answered by: srividya | Date: 11/5/2009
| Contact srividya
a const member of the class cannot be assigned a non const rvalue... thats why u pass a const obj... this is the reason Source: CoolInterview.com
Answered by: varun | Date: 11/6/2009
| Contact varun
passing the objs as arguments by reference, allows NOT to call the zero argument constructor in cases <br>1. calling the copy const directly<br>2. when passing the objects to/frm funcs.<br>3. in case of assignment operator overloading<br>but when objs are passed as const reference ,it prevents the accident change of the passed object as argument<br> Source: CoolInterview.com
Answered by: sasi | Date: 11/11/2009
| Contact sasi
Copy constructor is called when one object is used to initialize other object. <br>Declaring this calls copy constructor during object to object initializing. Default bitwise copy is not done.<br>The const pointer is reference to the object on the right side. Source: CoolInterview.com
Answered by: kushboo | Date: 11/30/2009
| Contact kushboo
Please correct the question. It should be What is importance of const reference in copy constructor parameter? Source: CoolInterview.com
Answered by: Zahid | Date: 7/29/2010
| Contact Zahid
class MyClass{ public: MyClass(); MyClass(const MyClass& mc) { i_ = mc.i_; } .... protected: int i_; }
Is the example of copy constructor, the const here indicates that this constructor is an inspector kind of function this will not modify the object that is sent as parameter.
Is it not... Source: CoolInterview.com
Answered by: Pavani | Date: 8/23/2010
| Contact Pavani
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|