Define copy constructor? What is the use of copy constructor?

Questions by suryateja03

Showing Answers 1 - 4 of 4 Answers

vss

  • Jun 29th, 2006
 

A copy constructor is used to initialize a newly declared variable from an existing variableA variable is declared which is initialized from another object,A value parameter is initialized from its corresponding argument.f(p); // copy constructor initializes formal value parameter. An object is returned by a function.C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy.

  Was this answer useful?  Yes

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.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions