Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

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;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #create #copy #constructor #generic #array #class
ADD COMMENT
Topic
Name
9+7 =