Copy Constructor for Dynamic Arrays modified by Arithmetic Operator member functions
<p>Although copy constructors in this regard have been discussed before, I was unable to find an optimal solution to the following problem: How to invoke the copy constructor through the return of a class object containing arithmetic results (*,/,+) performed by member functions - as shown below?</p>

<pre><code>#include &lt;iostream&gt;
#include "DataContainer.hh"
using namespace std;

int main() {
   DATA_TYPE numbers1[] = {1,2,3,4,5,6,7,8,9,10}; 
   DATA_TYPE numbers2[] = {10,9,8,7,6,5,4,3,2,1};
   DataContainer *dataContainer1 = new DataContainer(numbers1,10);
   DataContainer *dataContainer2 = new DataContainer(numbers2,10);
   dataContainer1-&gt;printArray();
   dataContainer2-&gt;printArray();
   cout &lt;&lt; "&gt;&gt; Multiplication:" &lt;&lt; endl;
     (*dataContainer1 * (*dataContainer2)).printArray();
   cout &lt;&lt; "&gt;&gt; Addition:" &lt;&lt; endl;
     (*dataContainer1 + (*dataContainer2)).printArray();
   cout &lt;&lt; "&gt;&gt; Division:" &lt;&lt; endl;
     (*dataContainer1 / (*dataContainer2)).printArray();

   delete dataContainer1;
   delete dataContainer2;
   return 0;
}
</code></pre>

<p><br></p>

<pre><code>#ifndef DATA_CONTAINER_HH
#define DATA_CONTAINER_HH
#define DATA_TYPE float

class DataContainer {
public:
  DataContainer(void);
  DataContainer(DATA_TYPE *array, int size);
  DataContainer(const DataContainer&amp; dataContainer);
  DataContainer operator*(DataContainer);
  DataContainer operator/(DataContainer);
  DataContainer operator+(DataContainer);
  void printArray();

private:
  DATA_TYPE *m_array;
  int m_size;
};
#endif
</code></pre>

<p><br></p>

<pre><code>#include "DataContainer.hh"
#include &lt;cmath&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;
using namespace std;

DataContainer::DataContainer()
{
}
DataContainer::DataContainer(DATA_TYPE *array, int size)
{
   m_size = size;
   m_array = new DATA_TYPE[m_size];
   for(int i=0; i&lt;m_size; i++)
      m_array[i] = array[i];
}

DataContainer::DataContainer(const DataContainer&amp; dataContainer)
   : m_size(dataContainer.m_size), m_array(new DATA_TYPE[dataContainer.m_size])
{
   std::copy(dataContainer.m_array, dataContainer.m_array + dataContainer.m_size, m_array);
}

void DataContainer::printArray()
{
   for(int i=0; i&lt;m_size; i++)
      cout &lt;&lt; m_array[i] &lt;&lt; " ";
   cout &lt;&lt; endl;
}

DataContainer DataContainer::operator*(DataContainer container) {
   DATA_TYPE *temp;
   for (int i=0;i&lt;m_size;i++) temp[i] = m_array[i] * container.m_array[i];
   return DataContainer(temp, m_size);
}
DataContainer DataContainer::operator/(DataContainer container) {
   DATA_TYPE *temp;
   for (int i=0;i&lt;m_size;i++) temp[i] = m_array[i] / container.m_array[i];
   return DataContainer(temp, m_size);
}
DataContainer DataContainer::operator+(DataContainer container) {
   DATA_TYPE *temp;
   for (int i=0;i&lt;m_size;i++) temp[i] = m_array[i] + container.m_array[i];
   return DataContainer(temp, m_size);
}
</code></pre>

<p><br>
The output is as follows:</p>

<pre><code>1 2 3 4 5 6 7 8 9 10 
10 9 8 7 6 5 4 3 2 1 
&gt;&gt; Multiplication:
Segmentation fault
</code></pre>

<p><br>
It seems that the dynamic memory allocation does not work. Any help is greatly appreciated. Thanks so much.
-Manuel</p>
