|
INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C++
DETAILS
Question: What is difference between shallow copy and deep copy? Which is default?
Answer: When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want. In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator.
|
|
|
Category |
C++ Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8200 users |
Added on |
5/3/2016 |
Views |
72163 |
Rate it! |
|
|
Question:
What is difference between shallow copy and deep copy? Which is default?
Answer:
When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want. In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
What is virtual destructors? Why they are used?
|
View Answer
|
|
What do you mean by C++ access specifiers ?
|
View Answer
|
|
What are the advantages of operator overloading?
|
View Answer
|
|
What are the advantages of using cin and cout compared to scanf(...) and printf(...), respectively?
|
View Answer
|
|
What are static members and static functions?
|
View Answer
|
|
What are storage qualifiers in C++ ?
|
View Answer
|
|
What is the use of default constructor?
|
View Answer
|
|
What are tokens in C++?
|
View Answer
|
|
Define destuctors ?
|
View Answer
|
|
What is the difference between a template class and class template?
|
View Answer
|
|
What are the differences between new and malloc ?
|
View Answer
|
|
What is difference between template and macro ?
|
View Answer
|
|
What do you mean by early binding?
|
View Answer
|
|
How can we access protected and private members of a class?
|
View Answer
|
|
what should the 64-bit integer type on new,64-bit machines be?
|
View Answer
|
|
Applications of c++ object oriented programs?
|
View Answer
|
|
What is a mutable member?
|
View Answer
|
|
Define a constructor - What it is and how it might be called (2 methods).
|
View Answer
|
|
Anything wrong with this code? T *p = 0; delete p;
|
View Answer
|
|
Anything wrong with this code? T *p = new T[10]; delete p;
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C++ Interview Questions & Answers - Exam Mode /
Learning Mode
|