title: When the object destructed while I pass the argument by-value?
body: <p>given the following code:  </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class A {
  public:
      A() {
      }
      A(const A&amp; a) {
          cout &lt;&lt; "A copy ctor" &lt;&lt; endl;
      }
      virtual ~A() {
          cout &lt;&lt; "A dtor" &lt;&lt; endl;
      }
      virtual void type() const {
          cout &lt;&lt; "This is A" &lt;&lt; endl;
      }
  };
  class B: public A {
  public:
      virtual ~B() {
          cout &lt;&lt; "B dtor" &lt;&lt; endl;
      }
      virtual void type() const {
          cout &lt;&lt; "This is B" &lt;&lt; endl;
      }
  };
  A f(A a) {
      a.type();
      return a;
  }
  const A&amp; g(const A&amp; a) {
      a.type();
      return a;
  }
  int main() {
      A *pa = new B();
      cout &lt;&lt; "applying function f:" &lt;&lt; endl;
      f(*pa).type();
      cout &lt;&lt; "~~~ delete: ~~~" &lt;&lt; endl;
      delete pa;
      return 0;
  }
  </code></pre>
  
  <p>I get the following output:  </p>
  
  <blockquote>
    <p>applying function f:<br>
    A copy ctor<br>
    This is A<br>
    A copy ctor<br>
    <strong>This is A</strong><br>
    <strong>A dtor</strong><br>
    <strong>A dtor</strong><br>
    ~~~ delete: ~~~<br>
    B dtor<br>
    A dtor   </p>
  </blockquote>
  
  <p>But I don't understand something. It's seen that while we exists from <code>f</code> the object there is not destructed. Why it's not destructed? (After all, we get out of the scope of the function, so it has to be destroyed, no?)   </p>
  
  <p>Note: I emphasized the problematic lines (which I do not understand why it happens in this order)  </p>
  
  <p>We going to the function f, at this point, we call to the copy c'tor and printed "A copy ctor" (Ok) , after it, we returns to the function f and going to type() , and then, it's prints "This is A" , now we escpaes from the function f so we calls to the copy c'tor , hence will be printed "A copy ctor" , But, now, what the destructor is not called (while we escape from the function f)..?  </p>
  
  <p>I thought that the output will be the following (according to the description above) :  </p>
  
  <blockquote>
    <p><strong>applying function f:</strong><br>
    <strong>A copy ctor</strong><br>
    <strong>This is A</strong><br>
    <strong>A copy ctor</strong> (escape from <code>type</code>)<br>
    <strong>A dtor</strong> (escape from <code>type</code>)<br>
    <strong>This is A</strong> (at the main)<br>
    <strong>~~~ delete: ~~~</strong><br>
    <strong>B dtor</strong><br>
    <strong>A dtor</strong> </p>
  </blockquote>