title: Is assignment operator always necessary when there is a copy constructor?
body: <p>I am learning the constructor and destructor, and have learned the <a href="https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/4172724#4172724">rule of three</a>. </p>
  
  <p>I am now playing a small example from <a href="http://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm" rel="nofollow noreferrer">tutorialspoint</a>. I notice the example doesn't have an assignment operator, but the code somehow works well.
  e.g., as to <code>Line a(b)</code>, when I change the content in a, e.g., *(a.ptr), *(b.ptr) is not changed.</p>
  
  <p>I also write one assignment operator (commented), the code also works.</p>
  
  <p>Now I am confused. It seems on some occasions, only copy constructor is enough. Could anyone comment on this to help me know better the mechanism of memory allocation involved in calling copy constructor?</p>
  
  <p>Thanks!</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class Line
  {
     public:
        int getLength( void );
        Line( int len );             // simple constructor
        Line( const Line &amp;obj);  // copy constructor
        ~Line();                     // destructor
        void doubleLength(void);
       Line &amp;operator=(const Line &amp;);
  
     private:
        int *ptr;
  };
  
  // Member functions definitions including constructor
  Line::Line(int len)
  {
      cout &lt;&lt; "Normal constructor allocating ptr" &lt;&lt; endl;
      // allocate memory for the pointer;
      ptr = new int; //simply allocates memory for one integer, and returns a pointer to it.
      *ptr = len;
  }
  
  Line::Line(const Line &amp;obj)
  {
      cout &lt;&lt; "Copy constructor allocating ptr." &lt;&lt; endl;
      ptr = new int;
     *ptr = *obj.ptr; // copy the value
  }
  
  //  // copy assignment operator, added by me
  //  Line&amp; Line::operator=(const Line&amp; that)
  //  {
  //      if (this != &amp;that)
  //      {
  //          delete ptr;
  //          // This is a dangerous point in the flow of execution!
  //          // We have temporarily invalidated the class invariants,
  //          // and the next statement might throw an exception,
  //          // leaving the object in an invalid state :(
  //          this-&gt;ptr = new int;
  //          this-&gt;ptr = that.ptr;
  
  //      }
  //      return *this;
  //  }
  
  
  Line::~Line(void)
  {
      cout &lt;&lt;  "Freeing memory " &lt;&lt; ptr &lt;&lt; endl;
      delete ptr;
  }
  int Line::getLength( void )
  {
      return *ptr;
  }
  
  void Line::doubleLength(void)
  {
      *ptr = *ptr * 2;
  }
  
  void display(Line obj)
  {
     cout &lt;&lt; "Length of line : " &lt;&lt; obj.getLength() &lt;&lt;endl;
  }
  
  // Main function for the program
  int main( )
  {
     Line line1(10);
  
  //     Line line2 = line1; // This also calls copy constructor
     Line line2(line1);   // performed by copy assignment operator    
     line2.doubleLength();
     display(line1);
     display(line2);
  
  
     return 0;
  }
  </code></pre>
  
  <p>I get the output:</p>
  
  <pre><code>Normal constructor allocating ptr
  Copy constructor allocating ptr.
  Copy constructor allocating ptr.
  Length of line : 10
  Freeing memory 0x836c028
  Copy constructor allocating ptr.
  Length of line : 20
  Freeing memory 0x836c028
  Freeing memory 0x836c018
  Freeing memory 0x836c008
  </code></pre>