What is a "Copy Constructor"?

Answers were Sorted based on User's Feedback



What is a "Copy Constructor"?..

Answer / phool chand

A Copy Constructor constructs a new object as a copy of an existing object of the same type. Frequently copy constructors do a 'deep copy' of the object. X( const X& X_object ){...}; is a copy constructor for class X.

Deep Copy vs. Shallow Copy:

a shallow copy simply copies the contents of an object directly - if the object contains pointers, both the old copy and the new copy contain pointers to the same actual item. In a deep copy, when an object contains a pointer, a new copy of whatever the pointer points AT is created and the new object contains a pointer to the newly created copy of the item.

Why are deep copies important? If you carry out a shallow copy you end up with two pointers to the same item. If that item is an object with a destructor, this generally means you'll end up calling the destructor for that item twice, which will generally cause problems.

Unfortunately, most don't know to ask this question directly: the symptom is generally heap corruption which is hard to track down directly since there it has many possible causes.

Is This Answer Correct ?    0 Yes 0 No

What is a "Copy Constructor"?..

Answer / hrpynux@gmail.com

In the C++ programming language, a copy constructor is a special constructor for creating a new object as a copy of an existing object.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

What apps are written in c++?

0 Answers  


how is returning structurs from functions?Show an eg?

1 Answers   GE,


What is virtual destructors? Why they are used?

1 Answers  


Is c++ map a hash table?

0 Answers  


Write a Program to find the largest of 4 no using macros.

0 Answers  






What is insertion sorting?

0 Answers  


Write a function which takes a character array as input and reverses it in place.

2 Answers   Lehman Brothers, Vision Infotech,


What is the best free c++ compiler for windows?

0 Answers  


Is rust better than c++?

0 Answers  


Explain the term memory alignment?

0 Answers  


Live example for static function?

1 Answers   Infosys,


Difference between Top down and bottom up approaches for a given project ?

14 Answers   BSNL, CSC, HCL, HP, IIT, Infosys, Siemens,


Categories