title: C++ copy constructor: field with list of pointers
body: <p>I am trying to create a copy constructor for my class A that contains a list of pointers of type B. The way I was trying to do it is as follows:</p>
  
  <pre><code>class A
  {
   private:
     list&lt;B*&gt; myList;
     list&lt;B*&gt;::iterator iter;
  
  public:
    A()
    {
     }
  
   // Copy constructor
   A(const A &amp;src)
    {
      myList.assign(src.myList.begin(), src.myList.end());
    }
  
  A&amp; operator= (const A &amp;src);
  
   };
  
  A&amp; A::operator= (const A &amp;src)
  {
  // do the copy
  myList.assign(src.myList.begin(), src.myList.end());
  
  // return the existing object
  return *this;
  }
  </code></pre>
  
  <p>Now, my problem is that if I do this:</p>
  
  <pre><code>A=a1;
  
  //call function that will change a1
  
  A  a2(a1);
  
  //call function that will change a1
  </code></pre>
  
  <p>Then after calling this last function that changes a1, I notice that a2 has changed too. I thought that the copy constructor is supposed to prevent this from happening, but I can not figure out what I am doing wrong in my code.</p>