title: Why is the copy ctor used in this code?
body: <pre><code>class A
  {
   public:
    A(const int n_);
    A(const A&amp; that_);
    A&amp; operator=(const A&amp; that_);
  };
  
  A::A(const int n_)
  { cout &lt;&lt; "A::A(int), n_=" &lt;&lt; n_ &lt;&lt; endl; }
  
  A::A(const A&amp; that_)    // This is line 21
  { cout &lt;&lt; "A::A(const A&amp;)" &lt;&lt; endl; }
  
  A&amp; A::operator=(const A&amp; that_)
  { cout &lt;&lt; "A::operator=(const A&amp;)" &lt;&lt; endl; }
  
  int foo(const A&amp; a_)
  { return 20; }
  
  int main()
  {
    A a(foo(A(10)));    // This is line 38
    return 0;
  }
  </code></pre>
  
  <p>Executing this code gives o/p:  </p>
  
  <blockquote>
    <p>A::A(int), n_=10  <br/>
    A::A(int), n_=20  <br/></p>
  </blockquote>
  
  <p>Apparently the copy constructor is never called.  </p>
  
  <pre><code>class A
  {
   public:
    A(const int n_);
    A&amp; operator=(const A&amp; that_);
   private:
    A(const A&amp; that_);
  };
  </code></pre>
  
  <p>However, if we make it private, this  compile error occurs:  </p>
  
  <blockquote>
    <p>Test.cpp: In function ‘int main()’:  <br/>
    Test.cpp:21: error: ‘A::A(const A&amp;)’ is private  <br/>
    Test.cpp:38: error: within this context  </p>
  </blockquote>
  
  <p>Why does the compiler complain when it doesn't actually use the copy constructor?<br>
  I am using gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)</p>