c++ assignment operator with pointers code example
Example: assignment operator with pointers c++
class Array
{
public:
Array(int N)
{
size = N;
arr = new int[N];
}
~Array()
{
delete[] arr;
}
Array(const Array& arr2)
{
size = arr2.size;
arr = new int[size];
std::memcpy(arr, arr2.arr, size);
}
Array& operator=(const Array& arr2)
{
if (this == &arr2)
return *this;
if (arr != NULL)
delete[] arr;
size = arr2.size;
arr = new int[size];
std::memcpy(arr, arr2.arr, size);
return *this;
}
private:
int size;
int *arr;
};