Answers for "how to create a copy constructor for generic array class in c++"

C++
1

how to create a copy constructor for generic array class in c++

template <typename Type>
Array<Type>::Array(const Array<Type>& data) // copy constructor 
{
    m_size = data.m_size; // set m_size to the size of the data which should be copied
    m_data = new Type[m_size]; // allocate memory on the heap for m_data to be copied from the new data array 
    for (int i = 0; i < m_size; ++i)
    {
        m_data[i] = data.m_data[i]; // copy each element one at a time 
    }
    cout << "Copy called" << endl;
}
Posted by: Guest on March-04-2022

Code answers related to "how to create a copy constructor for generic array class in c++"

Browse Popular Code Answers by Language