Previously, we had created a class in C++ called Dynamic Array with a number of utility functions (refer to Dynamic Array | Vector). But what would have happen if you had two objects of that class e.g.
DynamicArray obj1;
DynamicArray obj2;
And both had some data, say:
obj1 : 1,2,3,4,5,6
obj2 : 6,7,8,9,10,11
and if you do something like this :
obj1 = obj2;
This would result in calling for the standard assignment operator which will point the arr* in obj1 to the same location where arr* in obj2 is pointing, i.e. now both the objects are pointing to the same memory address in heap.
The image above shows the relation. The orange arrows show the data pointed by the arr* of each objects before assignment and the green pointers show the relation after the assignment i.e. now both the obj1 and obj2 are pointing to the arr {6,7,8,9,10,11} and the address of arr {1,2,3,4,5,6} has been lost (over-written). This results in two big errors:
The code:
void operator = (const DynamicArray & obj)
{
if (arr == nullptr)
delete [] arr; //since we are calling it as obj1 = obj2 so obj2 is the argument passed
//and obj1 one is the object that we are inside
//which means when I write arr*, it is the private member of obj1
arr = new int[obj.cap]; //to access functions or members for obj2, the argument,e.g. its capacity we write obj.cap (obj is the name of the argument... it is obj not obj1)
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
cap = obj.cap;
}
That's all for today. Now whenever you use = operator for the objects of this class, your own operator will be summoned. Isn't it cool? :P
You can also try making + , - operator or == and != operators. Try it yourself! ^^
Thank you for reading! Any questions or confusions? Comment below!
DynamicArray obj1;
DynamicArray obj2;
And both had some data, say:
obj1 : 1,2,3,4,5,6
obj2 : 6,7,8,9,10,11
and if you do something like this :
obj1 = obj2;
This would result in calling for the standard assignment operator which will point the arr* in obj1 to the same location where arr* in obj2 is pointing, i.e. now both the objects are pointing to the same memory address in heap.
- Leak of memory
- Double deletion of the same data
What is Deep Copying?
Deep copying is copying element by element. In this method, there are the following steps:
- If the object to be copied into (e.g. obj1 here) is not empty, first deallocate it.
- Then reserve a new space on the heap memory.
- Copy one by one elements (e.g. from obj2 to obj1 ).
Now let's write the code for assignment operator of DynamicArray class.
Code:
The prototype of it looks like :
<return type> operator <operator symbol> (<arguments>)
void operator = (const DynamicArray & obj)
//void because this operator won't allow cascading i.e. no such thing obj1 = obj2 = obj3
//const because we are not gonna make any changes in the argument passed
//& (passing by reference) because it avoids copying (refer to Passing by Reference in Functions)
The code:
void operator = (const DynamicArray & obj)
{
if (arr == nullptr)
delete [] arr; //since we are calling it as obj1 = obj2 so obj2 is the argument passed
//and obj1 one is the object that we are inside
//which means when I write arr*, it is the private member of obj1
arr = new int[obj.cap]; //to access functions or members for obj2, the argument,e.g. its capacity we write obj.cap (obj is the name of the argument... it is obj not obj1)
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
cap = obj.cap;
}
That's all for today. Now whenever you use = operator for the objects of this class, your own operator will be summoned. Isn't it cool? :P
You can also try making + , - operator or == and != operators. Try it yourself! ^^
Thank you for reading! Any questions or confusions? Comment below!
0 comments:
Post a Comment