What is a "Copy Constructor"?
Answers were Sorted based on User's Feedback
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 |
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 |
How much is size of struct having 1 char & 1 integer?
How to declare an array of pointers to integer?
describe private access specifiers?
What data encapsulation is in c++?
Differentiate between the manipulator and setf( ) function?
Explain virtual class?
How do you differentiate between overloading the prefix and postfix increments?
What do you mean by vtable and vptr in c++?
What are the various access specifiers in c++?
What is optimization in c++? when using volatile.optimization is not possible..what does this mean?
What is polymorphism and its type in c++?
Does c++ cost money?