title: Pointer member is not initialized in copy constructor
body: <p>In my application</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class TestClassA
  {
  
  public:
      int* m_ptr;
      TestClassA(int a)
      {
          m_ptr = new int(a);
          std::cout &lt;&lt; "Constructor. this: " &lt;&lt; this &lt;&lt; " m_ptr: " &lt;&lt; m_ptr &lt;&lt; std::endl;
      }
  
      TestClassA(const TestClassA&amp; copy)
      {
          std::cout &lt;&lt; "Copy Constructor. copy: " &lt;&lt; &amp;copy &lt;&lt; " -&gt; this: " &lt;&lt; this &lt;&lt; std::endl;
          std::cout &lt;&lt; "Copy Constructor. old this-&gt;m_ptr: " &lt;&lt; m_ptr &lt;&lt; std::endl;
          delete m_ptr; // not initialized pointer
          m_ptr = new int;
          std::cout &lt;&lt; "Copy Constructor. new this-&gt;m_ptr: " &lt;&lt; m_ptr &lt;&lt; std::endl;
          *m_ptr = *copy.m_ptr;
      }
  
      // passing by value, thus a copy constructor calls first
      TestClassA&amp; operator=(TestClassA tmp)
      {
          std::cout &lt;&lt; "Copy assignment " &lt;&lt; this &lt;&lt; " &lt;- " &lt;&lt; &amp;tmp &lt;&lt; std::endl;
          std::swap(m_ptr, tmp.m_ptr);
          return *this;
      }
  
  
      ~TestClassA()
      {
          std::cout &lt;&lt; "Destructor " &lt;&lt; this &lt;&lt; std::endl;
          delete m_ptr;
          m_ptr = nullptr;
      }
  };
  
  void testAssignment()
  {
      TestClassA tca1(1);
      std::cout &lt;&lt; "tca1.m_ptr: " &lt;&lt; tca1.m_ptr &lt;&lt; std::endl;
  
      TestClassA tca2(2);
      std::cout &lt;&lt; "tca2.m_ptr: " &lt;&lt; tca2.m_ptr &lt;&lt; std::endl;
      tca2 = tca1;
  }
  
  int main()
  {
      testAssignment();
      return 0;
  }
  </code></pre>
  
  <p>When I call assignment operator receiving arguments by value, copy constructor calls. I guess it is to create a temporary variable and to copy the state of <em>tcs1</em> to it. The issue is that <em>m_ptr</em> member of this temporary is not initialized, so I can't delete previous <em>m_ptr</em> value to write a new one. What is the proper way of implementing copy constructor in this case?</p>