Matt Senko Matt Senko - 2 months ago 19
C++ Question

how copy constructors work

#include <iostream>
#include <cassert> // for assert

class IntArray
{
private:
int m_length = 0;
int *m_array = nullptr;

public:
IntArray(int length):
m_length(length)
{
if (length <= 0)
assert("IntArray length should be a positive integer");

m_array = new int[m_length] { 0 };
}

// Copy constructor that does a deep copy
IntArray(const IntArray &array):
m_length(array.m_length)
{
// Allocate a new array
m_array = new int[m_length];

// Copy elements from original array to new array
for (int count = 0; count < array.m_length; ++count)
m_array[count] = array.m_array[count];
}

~IntArray()
{
delete[] m_array;
}

// If you're getting crazy values here you probably forgot to do a deep copy in your copy constructor
friend std::ostream& operator<<(std::ostream &out, const IntArray &array)
{
for (int count = 0; count < array.m_length; ++count)
{
std::cout << array.m_array[count] << ' ';
}
return out;
}

int& operator[] (const int index)
{
assert(index >= 0);
assert(index < m_length);
return m_array[index];
}

// Assignment operator that does a deep copy
IntArray& operator= (const IntArray &array)
{
// self-assignment guard
if (this == &array)
return *this;

// If this array already exists, delete it so we don't leak memory
delete[] m_array;

m_length = array.m_length;

// Allocate a new array
m_array = new int[m_length];

// Copy elements from original array to new array
for (int count = 0; count < array.m_length; ++count)
m_array[count] = array.m_array[count];

return *this;
}

};

IntArray fillArray()
{
IntArray a(5);
a[0] = 5;
a[1] = 8;
a[2] = 2;
a[3] = 3;
a[4] = 6;

return a;
}

int main()
{
IntArray a = fillArray();

// If you're getting crazy values here you probably forgot to do a deep copy in your copy constructor
std::cout << a << '\n';

IntArray b(1);
a = a;
b = a;

// If you're getting crazy values here you probably forgot to do a deep copy in your assignment operator
// or you forgot your self-assignment check
std::cout << b << '\n';

return 0;
}


so im trying to understand this code. I know the copy constructor is going to be called for IntArray a = FillArray(). However I do not know how IntArray(const IntArray &array) works. I understand the syntax, but dont understand where array.m_length is coming from. Nor am I understanding how these values get returned to IntArray a. Please help.

Answer

m_length is a class member of type "int".

This code:

IntArray(const IntArray &array): m_length(array.m_length)

declares a function with one parameter named "array". The semicolon denotes a constructor initializer list. m_length is a class member called out to be constructed in this list. m_length is an "int", so int's copy constructor is called automatically when this function is invoked. array.m_length comes from "array" which is the argument being passed in your copy constructor.