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 is scope in c++ with example?

0 Answers  


How does c++ structure differ from c++ class?

0 Answers  


What are stacks? Give an example where they are useful.

0 Answers  


What is the function of I/O library in C++ ?

0 Answers   HCL,


What are the main features of c++?

0 Answers  






Explain the concept of friend function in c++?

0 Answers  


class Foo { const int x; protected: Foo(int f); ~Foo(); }; Foo f; Referring to the sample code above, why will the class declaration not compile? a) The variable x is const. b) The destructor is protected. c) The destructor is not public. d) The constructor is protected. e) There is no default constructor.

5 Answers   Quark,


What does getch() do according to the ANSI C++ standard a) Reads in a character b) Checks the keyboard buffer c) Nothing in particular (Its not defined there)

0 Answers  


How new/delete differs from malloc()/free?

0 Answers  


What is the difference between reference type and pointers.

4 Answers   HCL,


What is the role of C++ shorthand's?

0 Answers   TCS,


Why use of template is better than a base class?

0 Answers  


Categories