GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Programming  >  C++

 Print  |  
Question:  Define copy constructor? What is the use of copy constructor?



September 09, 2009 10:29:11 #2
 rajangidwani   Member Since: July 2009    Total Comments: 5 

RE: Define copy constructor? What is the use of copy constructor?
 
Copy constructor is
1. a constructor function with the same name as the class
2. used to make deep copy of objects.


There are 3 important places where a copy constructor is called.
1. When an object is created from another object of the same type
2. When an object is passed by value as a parameter to a function
3. When an object is returned from a function.
 

If a copy constructor is not defined in a class, the compiler itself defines one. This will ensure a shallow copy. If the class does not have pointer variables with dynamically allocated memory, then one need not worry about defining a copy constructor. It can be left to the compiler's discretion.
     

 

Back To Question