title: Returning object with pointer in it
body: <p>I have created this simple object in C++:</p>
  
  <pre><code>class Array
  {
      int Size;
      int * pArr;
  
  public:
      explicit Array(int InSize = 0)
      {
          Size=InSize;
          if (Size) 
              pArr = new int[Size];
          else 
              pArr = NULL;
      }
  
      Array(const Array &amp; InArray)
      {       
          Size=InArray.Size;
          if (Size)
              pArr=new int[Size];
          else
              pArr=NULL;
      }
  
      ~Array()
      {
          if (pArr)
          {
              delete []pArr;
              Size=0;
          }     
      }
  };
  </code></pre>
  
  <p>I am trying to overload the + operator in the following manner:</p>
  
  <pre><code>Array operator+(const Array &amp;LOpArray,const Array &amp;ROpArray)
  {
      int MinSize=LOpArray.Size&lt;ROpArray.Size ? LOpArray.Size : ROpArray.Size ;
      Array ResArray;
  
      if (LOpArray.Size&lt;ROpArray.Size)
          ResArray=ROpArray;
      else
          ResArray=LOpArray;
      for (int i=0;i&lt;MinSize;i++)
          ResArray.pArr[i]=LOpArray.pArr[i]+ROpArray.pArr[i];
      return(ResArray);
  }
  </code></pre>
  
  <p>Assignment is defined as:</p>
  
  <pre><code>Array&amp; Array::operator=(const Array &amp;ROpArray)
  {
      if (pArr!=ROpArray.pArr)
      {          
          Size=ROpArray.Size;
          pArr=new int[Size];
          for (int i=0;i&lt;Size;i++)
              pArr[i]=ROpArray.pArr[i];
      }
      return (*this);
  }
  </code></pre>
  
  <p>The problem is when I use it like :</p>
  
  <pre><code>Array A1(20),A2(8),A4;
  
  ....//initializing pointer contents
  
  A4=A1+A2;
  </code></pre>
  
  <p>then A4 has correct values (i.e. size=20 and <strong>new</strong>,non NULL, int pointer)
  but all the pointed values have garbage values.
  debugging showed that in the operator+ function values are actually correct
  but they are deleted when returning from the operator+ function</p>
  
  <p>What am I doing wrong?</p>