title: What does String do that I'm not doing? c++11
body: <p>I am still new to c++, so bear with me.</p>
  
  <p>I was trying to learn more about how std::move works and I saw an example where they used std::move to move the string to a different function and then showed using std::cout that no string remained. I thought cool, let's see if I can make my own class and do the same:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class integer
  {
  private:
      int *m_i;
  public:
      integer(int i=0) : m_i(new int{i})
      {
          std::cout &lt;&lt; "Calling Constructor\n";
      }
  
      ~integer()
      {
          if(m_i != nullptr) {
              std::cout &lt;&lt; "Deleting integer\n";
              delete m_i;
              m_i = nullptr;
          }
      }
  
      integer(integer&amp;&amp; i) : m_i(nullptr)  // move constructor
      {
          std::cout &lt;&lt; "Move Constructor\n";
          m_i = i.m_i;
          i.m_i = nullptr;
      }
      integer(const integer&amp; i) : m_i(new int) {  // copy constructor
          std::cout &lt;&lt; "Copy Constructor\n";
          *m_i = *(i.m_i);
      }
  //*
      integer&amp; operator=(integer&amp;&amp; i) {   // move assignment
          std::cout &lt;&lt; "Move Assignment\n";
          if(&amp;i != this) {
              delete m_i;
              m_i = i.m_i;
              i.m_i = nullptr;
          }
          return *this;
      }
      integer&amp; operator=(const integer &amp;i) {   // copy assignment
          std::cout &lt;&lt; "Copy Assignment\n";
          if(&amp;i != this) {
             m_i = new int;
             *m_i = *(i.m_i);
          }
         return *this;
      }
      int&amp; operator*() const { return *m_i; }
      int* operator-&gt;() const { return m_i; }
  
      bool empty() const noexcept {
         if(m_i == nullptr) return true;
         return false;
      }
  
      friend std::ostream&amp; operator&lt;&lt;(std::ostream &amp;out, const integer i) {
         if(i.empty()) {
             std::cout &lt;&lt; "During overload, i is empty\n";
             return out;
         }
      out &lt;&lt; *(i.m_i);
      return out;
      }
  };
  
  void g(integer i) { std::cout &lt;&lt; "G-wiz - "; std::cout &lt;&lt; "The g value is " &lt;&lt;  i &lt;&lt; '\n'; }
  void g(std::string s) { std::cout &lt;&lt; "The g value is " &lt;&lt; s &lt;&lt; '\n'; }
  
  int main()
  {
      std::string s("Hello");
  
      std::cout &lt;&lt; "Now for string\n";
      g(std::move(s));
      if(s.empty()) std::cout &lt;&lt; "s is empty\n";
      g(s);
      std::cout &lt;&lt; "\nNow for integer\n";
      integer i = 77;
      if(!i.empty()) std::cout &lt;&lt; "i is " &lt;&lt; i &lt;&lt; '\n';
      else std::cout &lt;&lt; "i is empty\n";
      g(i);
      std::cout &lt;&lt; "Move it\n";
      g(std::move(i));  // rvalue ref called
      if(!i.empty()) std::cout &lt;&lt; "i is " &lt;&lt; i &lt;&lt; '\n';
      else std::cout &lt;&lt; "i is empty\n";
      g(i);
  
      return 0;
  }
  </code></pre>
  
  <p>And this is my output:</p>
  
  <pre><code>Now for string
  The g value is Hello
  s is empty
  The g value is
  
  Now for integer
  Calling Constructor
  Copy Constructor
  i is 77
  Deleting integer
  Copy Constructor
  G-wiz - Copy Constructor
  The g value is 77
  Deleting integer
  Deleting integer
  Move it
  Move Constructor
  G-wiz - Copy Constructor
  The g value is 77
  Deleting integer
  Deleting integer
  i is empty
  Copy Constructor
  
  Process returned 255 (0xFF)   execution time : 7.633 s
  Press any key to continue.
  </code></pre>
  
  <p>As you can see, it crashes when it enters g the second time, never even getting to the operator&lt;&lt;() function. How is it that the empty std::string s can be passed to g where my empty integer i crashes the program?</p>
  
  <p>Edit: Fixed new int vs. new int[] error. Thanks n.m. </p>