title: When creating a second object in C++ through the first object, where does the first object go?
body: <p>I am currently reading the second edition of <em>C++: A Beginner's Guide</em> by <em>Herbert Schildt</em>. </p>
  
  <p>In Module 9.4, he talks about returning objects:</p>
  
  <blockquote>
    <p>Just as objects can be passed to functions, functions can return objects. To return an object, first declare
    the function as returning a class type. Second, return an object of that type using the normal return
    statement. The following program has a member function called mkBigger( ). It returns an object that
    gives val a value twice as large as the invoking object.</p>
  </blockquote>
  
  <p>This is the 'following program' he mentions:</p>
  
  <pre><code>// Returning objects.
  #include &lt;iostream&gt;
  using namespace std;
  
  class MyClass {
  int val;
  public:
      // Normal Constructor.
      MyClass(int i) {
          val = i;
          cout &lt;&lt; "Inside constructor\n";
      }
  
      ~MyClass() {
      cout &lt;&lt; "Destructing\n";
      }
  
      int getval() { return val; }
  
      // Return an object.
      MyClass mkBigger() {
          Myclass o(val * 2); // mkBigger() returns a MyClass object.
  
          return o;
      }
  };
  
  void display(MyClass ob)
  {
      cout &lt;&lt; ob.getval() &lt;&lt; '\n';
  }
  
  int main()
  {
      cout &lt;&lt; " Before Constructing a.\n";
      MyClass a;
      cout &lt;&lt; "After constructing a.\n\n";
  
      cout &lt;&lt; "Before call to display.\n";
      display(a);
      cout &lt;&lt; "After display() returns.\n\n";
  
      cout &lt;&lt; "Before call to mkBigger().\n";
      a = a.mkBigger();
      cout &lt;&lt; "After mkBigger() returns.\n\n";
  
      cout &lt;&lt; "Before second call to display.\n";
      display(a);
      cout &lt;&lt; "After display() returns.\n\n";
  
      return 0;
  }
  </code></pre>
  
  <p>This gives us the following output:</p>
  
  <pre><code>Before Constructing a.
  Inside constructor
  After constructing a.
  
  Before call to display.
  10
  Destructing
  After display() returns.
  
  Before call to mkBigger()
  Inside constructor
  Destructing
  Destructing
  After mkBigger() returns.
  
  Before second call to display.
  20
  Destructing
  After display() returns.
  
  Destructing
  </code></pre>
  
  <p>Schildt then goes on to explain that the reason there are two 'Destructing' messages during the mkBigger() call is because of the fact that:</p>
  
  <blockquote>
    <p>when an object is returned by a function, a temporary object is automatically created, which holds the return value. It is this object that is actually returned by the function. After the value has been returned, this object is destroyed.</p>
  </blockquote>
  
  <p>I was actually surprised there wasn't <em>3</em> 'Destructing' messages. I have the following issue: Given the definition of mkBigger(), a new MyClass instance is created, and it is that instance that is returned and placed in the address of <strong>a</strong>. Thus, when doing </p>
  
  <pre><code>a = a.mkBigger();
  </code></pre>
  
  <p>My impression is thus that the original object previously held in <strong>a</strong> is no longer referenced by <strong>a</strong>. Is this correct? If so, I then have the following issues:</p>
  
  <p>I was told C++ has some minute notions of garbage collection. Would that object thus be garbage-collected? where is this object now? Is this an example of the possible feared memory leaks that many mention when talking about the 'dangers' of C++?</p>