Search
 
SCRIPT & CODE EXAMPLE
 

CPP

assignment operator with pointers c++

class Array
{
public:
    Array(int N)
    {
         size = N;
         arr = new int[N];
    }

    //destructor
    ~Array()
    {
        delete[] arr;
    }

    //copy constructor
    Array(const Array& arr2)
    {
        size = arr2.size;
        arr = new int[size];
        std::memcpy(arr, arr2.arr, size);
    }

    //overload = operator
    Array& operator=(const Array& arr2) 
    {
        if (this == &arr2)
            return *this; //self assignment
        if (arr != NULL)
            delete[] arr; //clean up already allocated memory

        size = arr2.size;
        arr = new int[size];
        std::memcpy(arr, arr2.arr, size);
        return *this;
    }

private:
    int size;    //array elements
    int *arr;    //dynamic array pointer
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: assign one vector to another c++ 
Cpp :: tabeau dynamique c++ 
Cpp :: maximum subarray leetcode c++ 
Cpp :: hide window c++ 
Cpp :: c++ memset 
Cpp :: Start mongodb community server 
Cpp :: string append at position c++ 
Cpp :: erase range vector c++ 
Cpp :: compare function in c++ 
Cpp :: memset in cpp 
Cpp :: count c++ 
Cpp :: copy constructor for vector c++ 
Cpp :: memcpy in cpp 
Cpp :: assign value to a pointer 
Cpp :: raspberry pi mount external hard drive 
Cpp :: convert uppercase to lowercase 
Cpp :: c++ stl 
Cpp :: set to vector c++ 
Cpp :: c++ read entire file into a variable 
Cpp :: heapsort 
Cpp :: Code debut C++ 
Cpp :: c++ graphics online compiler 
Cpp :: strcmp in c++ header file 
Cpp :: C++ Modified Data Types List 
Cpp :: How To Calculate 1+1 in c++ 
Cpp :: linq select where string equals "String" 
Cpp :: Missing GL version 
Cpp :: Polycarp found a rectangular table consisting of n rows and m columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": codeforces solution 
Cpp :: vermífugo significado 
Cpp :: Buy 2 Get 1 Free codechef solution in c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =