title: When are copy constructors and destructors called, and why?
body: <p>The code is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class P_Node {
      friend class Picture;
  protected:
      P_Node() : use(1) {}
      virtual ~P_Node() {}
  private:
      int use;
  };
  
  class Picture {
      friend Picture frame(const Picture&amp;);
  public:
      Picture() : p(new P_Node) {
          std::cout &lt;&lt; "Constructor\t" &lt;&lt; "Picture::Picture()" &lt;&lt; "\tcalled" &lt;&lt; std::endl;
          std::cout &lt;&lt; "Picture p count\t" &lt;&lt; p-&gt;use &lt;&lt; std::endl;
      }
      Picture(const Picture&amp; orig) : p(orig.p) {
          std::cout &lt;&lt; "Copy Constructor\t" &lt;&lt; "Picture::Picture(const Picture&amp;)" &lt;&lt; "\tcalled" &lt;&lt; std::endl;
          std::cout &lt;&lt; "Picture p count\t" &lt;&lt; p-&gt;use &lt;&lt; std::endl;
          orig.p-&gt;use++;
      }
      ~Picture() {
          std::cout &lt;&lt; "Destructor\t" &lt;&lt; "Picture::~Picture()" &lt;&lt; "\tcalled" &lt;&lt; std::endl;
          std::cout &lt;&lt; "Picture p count before decrease\t" &lt;&lt; p-&gt;use &lt;&lt; std::endl;
          if(--p-&gt;use == 0) {
              std::cout &lt;&lt; "Picture p count after decrease\t" &lt;&lt; p-&gt;use &lt;&lt; std::endl;
              std::cout &lt;&lt; "Deleted" &lt;&lt; std::endl;
              delete p;
          }
      }
      Picture&amp; operator=(const Picture&amp; orig) {
          std::cout &lt;&lt; "operator=\t" &lt;&lt; "Picture&amp; Picture::operator=(const Picture&amp; orig)" &lt;&lt; "\tcalled" &lt;&lt; std::endl;
          std::cout &lt;&lt; "Picture p count before decrease\t" &lt;&lt; p-&gt;use &lt;&lt; std::endl;
          orig.p-&gt;use++;
          if(--p-&gt;use == 0) {
              std::cout &lt;&lt; "Picture p count after decrease\t" &lt;&lt; p-&gt;use &lt;&lt; std::endl;
              std::cout &lt;&lt; "Deleted" &lt;&lt; std::endl;
              delete p;
          }
          p = orig.p;
          return *this;
      }
  private:
      Picture(P_Node* p_node) : p(p_node) {
          std::cout &lt;&lt; "Picture::Picture(P_Node* p_node)\tcalled" &lt;&lt; std::endl;
      }
      P_Node *p;
  };
  
  class Frame_Pic : public P_Node {
      friend Picture frame(const Picture&amp;);
  private:
      Frame_Pic(const Picture&amp; pic) : p(pic) {
          std::cout &lt;&lt; "Frame_Pic::Frame_Pic(const Picture&amp; orig)" &lt;&lt; "\tcalled" &lt;&lt; std::endl;
      }
      Picture p;
  };
  
  Picture frame(const Picture&amp; pic) {
      return new Frame_Pic(pic);
  }
  
  int main() {
      Picture my_pic;
      Picture temp = frame(my_pic);
      return 0;
  }
  </code></pre>
  
  <p>The result is:</p>
  
  <blockquote><pre>
  Constructor Picture::Picture()  called
  Picture p count 1
  Copy Constructor    Picture::Picture(const Picture&)    called
  Picture p count 1
  Frame_Pic::Frame_Pic(const Picture& orig)   called
  Picture::Picture(P_Node* p_node)    called
  Destructor  Picture::~Picture() called
  Picture p count before decrease 1
  Picture p count after decrease  0
  Deleted
  Destructor  Picture::~Picture() called
  Picture p count before decrease 2
  Destructor  Picture::~Picture() called
  Picture p count before decrease 1
  Picture p count after decrease  0
  Deleted
  </pre></blockquote>
  
  <p>I previously asked a question about memory management of this code, but after understanding the answers, I still have a problem with the destructor and the copy constructor. In my understanding, <code>Picture temp = frame(my_pic)</code> will call the copy constructor.</p>
  
  <p>Here comes the question:</p>
  
  <ol>
  <li>Why isn't the copy constructor called after <code>Picture temp = frame(my_pic)</code></li>
  <li>and why is the destructor called?</li>
  <li>In <code>Picture frame(const Picture&amp; pic)</code>, will the copy constructor be called if the function is called? I believe so, because it returns a 'Picture' by value.</li>
  <li>If I change <code>Picture frame(const Picture&amp; pic)</code> to <code>Picture frame(Picture p)</code> will the copy constructor called twice when the function is called?</li>
  <li>When will the copy constructor be called? Will it happen when the class is returned by a function by value? When then class is passed to a function by value?</li>
  <li>When will the destructor be called? Is it when each time a variable's lifetime is ended? Does that mean if I pass a variable to a function by value, its destructor will be called after the functions execution?</li>
  </ol>
  
  <p>I'm messed up with the copy constructor and the destructor right now, especially when I have a function with a return value, and some parameters all passed by values.</p>
  
  <p>Also, will anyone help me to write a comment on each line of the output strings? That would be very helpful.</p>