GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 90 of 203    Print  
what is importance of const. pointer in copy constructor?

  
Total Answers and Comments: 6 Last Update: July 22, 2007     Asked by: suryateja03 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: supriya ahire
 

        

      Hi  all,     

         when we try to  copy one object into another using copy constructer,we need to maintain the original copy of original object (which we are copying) so while passing object we make it constant and we pass it as a by reference.

  Thanks & Regards,

  Ms.Supriya Ahire.



Above answer was rated as good by the following members:
Ajay Kanse
July 25, 2006 04:31:52   #1  
supriya ahire        

RE: what is importance of const. pointer in copy const...

Hi all

when we try to copy one object into another using copy constructer we need to maintain the original copy of original object (which we are copying) so while passing object we make it constant and we pass it as a by reference.

Thanks & Regards

Ms.Supriya Ahire.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 03, 2006 02:47:18   #2  
Prashant Kumar        

RE: what is importance of const. pointer in copy const...
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
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 07, 2006 12:59:42   #3  
Anurag Verma        

RE: what is importance of const. pointer in copy const...
Copy constructor is called when the copy of an object is made. One of the case is when a function argument is passed by value. So if the signature of copy constructor will take the argument by value it will get into an infinite loop. So a reference is passed and it is made constant to remain unchanged in the copy constructor.
 
Is this answer useful? Yes | No
August 29, 2006 14:14:39   #4  
mohit12379 Member Since: March 2006   Contribution: 17    

RE: what is importance of const. pointer in copy const...

It is not Const. pointer in copy constructor but const Reference of object of same class type which we do pass in copy constructor.... and everyone has given pretty good answers for two questions

1) what is importance of const. reference object of same class type in copy const...

2) Why do we use reference of class instead of simple pass by value.

Again one query why dont we use pass by pointer in copy constructor so see following scenario

class ABC
{
public:
ABC(){} /*Simple Constructor*/
ABC(ABC * b) { }/*Copy Constructor*/
};

int main(int argc char* argv[]){
ABC * d1 NULL;
ABC d2 d1; /*Calling Copy constructor*/
return 0;
}

See above example if we take pointer type object in copy constructor

two flaws are here 1) We can assign NULL value to simple object of that class

2) see this line ABC d2 d1;

here d2 is ABC type while d1 is ABC* type means as per rule we are violating basic rules by allowing to assign simple type with pointer type.

If there is any pointer type member variable in side ABC class means DEEP Copy scenario like int * m_iVal; then it will crash out during calling copy constructor by passing NULL object... so stop such mistake at design time we do use const reference object of same class type in copy constructor as a parameter.Hoping this will clear you.


 
Is this answer useful? Yes | No
September 13, 2006 06:11:42   #5  
ramachandra        

RE: what is importance of const. pointer in copy const...

Copy constructor is useful when an object is initialized with another existing object or an object is passed by value to a function or an object is returned by value from a function. Shallow copy is nothing but copying members of a class by value so when an object is copied into another object all members will copied by value and if dynamic memory allocation is there the pointer will be copied but not the memory to which the pointer is pointing too. So the pointers in old and new pointing to same memory allocation. If the old objects is deleted memory to pointer is also deleted. In this case when the new object trying to access the same memory crash will happen. This can be avoided with copy constructor explicit declaration. in the below code I created memory to the char pointer name again in copy constrctor and copied the value. I hope this gives you better idea.. Thanks

#include<iostream.h>
#include<string.h>

class A

{public:

char *name;

A(){

name new char[20];

strcpy(name ramachandra );

}

A(const A &a)

{

name new char[20];

strcpy(name a.name);

}

};

void main()

{

A a;

cout<<a.name n;

A b a;

cout<<b.name n;

}


 
Is this answer useful? Yes | No
July 22, 2007 10:46:04   #6  
Indrajit Paul        

RE: what is importance of const. pointer in copy const...
Thank you everybody for giving the right answer to the question.

However the use of the term "pointer" in the question still leaves a lingering doubt in my mind as to whether the questioner really typoed a reference as a pointer? or did he MEAN a "pointer".

Dear Ramachandra the only thing I did not understand is how your explaination of shallow copy and deep copy helps answer the original question. If it does it's fine.. otherwise you ( and I suspect many other answerers like you who does not read the question properly before answering) are only wasting other peoples times.

Please please..and pretty please do read the question before answering.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape