Deep Copy and Shallow Copy in C++
The definition of deep copy and shallow copy is given below:
Creating a copy of object by copying data of all member variables as it is, is called shallow copy while creating an object by copying data of another object along with the values of memory resources resides outside the object but handled by that object, is called deep copy.
Let us first understand some basic points that you should aware of before knowing about shallow copy and deep copy in details.
How can we create a copy of an object?
1. Default copy constructor is defined by the compiler when object of any class is created and there is no copy constructor defined in the program.
2. In addition to copy constructor, C++ compiler also overload copy assignment operator which is implemented when an object (after declaration) is initialized using "=" with another object of same class.
For Example:- copy assignment operator in c++
#include<iostream> using namespace std; class DummyClass { private: int num1, num2; public: void setData(int x, int y) { num1 = x; num2 = y; } void showData() { cout << num1 << " " << num2 << endl; } }; int main() { DummyClass D1; D1.setData(5, 11); D1.showData(); //Copy constructor (created by compiler) will came in action here. DummyClass D2 = D1; D2.showData(); DummyClass D3; D3.setData(1, 11); D3.showData(); DummyClass D4; //Copy assignment operator overloaded by compiler came in action. D4 = D3; D4.showData(); return 0; }
Output:
5 11 5 11 1 11 1 11
In the above example, object D4 is created and then it is initialized with object D3. In this case, copy assignment operator which is overloaded by compiler itself is implemented.
Points To Remember:-
When we create a copy of an object:
1. copy constructor will be called or
2. implicit copy assignment operator will be called.
When will Copy Constructor call?
When you initialize an object with another object of same class during its declaration, copy constructor will be called. You may read about Copy Constructor in details here : Copy Constructors in C++ Programming
For Example:-
#include<iostream> using namespace std; class DummyClass { private: int num1, num2; public: void setData(int x, int y) { num1 = x; num2 = y; } void showData() { cout << num1 << " " << num2 << endl; } }; int main() { DummyClass D1; D1.setData(5, 11); D1.showData(); //Copy constructor (created by compiler) will came in action here. DummyClass D2 = D1; D2.showData(); }
When will Copy Assignment Operator call?
When you do not initialize an object during its declaration but initialize it after its declaration with another object of same class then copy assignment operator will be called.
For Example:-
#include<iostream> using namespace std; class DummyClass { private: int num1, num2; public: void setData(int x, int y) { num1 = x; num2 = y; } void showData() { cout << num1 << " " << num2 << endl; } }; int main() { DummyClass D1; D1.setData(5, 11); D1.showData(); DummyClass D2; //Copy assignment operator overloaded by compiler came in action. D2 = D1; D4.showData(); }
Deep Copy and Shallow Copy in C++
1. Creating a copy of object by copying data of all member variables as it is, it is called shallow copy. The compiler will do shallow copy by default.
2. Creating an object by copying data of another object along with the values of memory resources resides outside the object but handled by that object, it is called deep copy.
For Example:- deep copy and shallow copy in c++
#include<iostream> using namespace std; class DummyClass { private: int num1, num2; int *ptr; public: DummyClass() { ptr = new int; } void setData(int x, int y, int z) { num1 = x; num2 = y; *ptr = z; } void showData() { cout << "A = " << num1 << " B = " << num2 << endl; } //copy constructor -> it is responsible for deep copy. DummyClass(DummyClass &D) { num1 = D.num1; num2 = D.num2; *ptr = *(D.ptr); } //destructor -> to deallocate memory consumed by new pointer ptr. ~DummyClass() { delete ptr; } }; int main() { DummyClass D1; D1.setData(3, 5, 11); D1.showData(); //Copy constructor declared above will came in action and do deep copy. DummyClass D2 = D1; D2.showData(); }
you have missed one line in Deepcopy example Copy constructor, you have to create memory for the pointer in Copy Constructor- ptr= new int;
ReplyDeleteotherwise it leads to segmentation fault, Really good information, Thanks for the Article