title: C++: different behaviour of overloading function and operator
body: <p>I'm new to and learning C++.
  I have a question for function overload for classes.</p>
  
  <p>I have two exactly same code below except the function name.
  One is just a function and the other is operator.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using std::cout;
  using std::endl;
  
  class B;
  
  class A {
    public:
      void test(A const &amp;a) { cout &lt;&lt; "AA" &lt;&lt; endl; }
      void test(B const &amp;b) { cout &lt;&lt; "AB" &lt;&lt; endl; }
  };
  
  class B : public A {
    public:
      void test(A const &amp;a) { cout &lt;&lt; "BA" &lt;&lt; endl; }
  };
  
  int main() {
    B b1;
    B b2;
    b1.test(b2);
    return 0;
  }
  </code></pre>
  
  <p>This program prints</p>
  
  <pre><code>BA
  </code></pre>
  
  <p>And here is another program.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using std::cout;
  using std::endl;
  
  class B;
  
  class A {
    public:
      void operator=(A const &amp;a) { cout &lt;&lt; "AA" &lt;&lt; endl; }
      void operator=(B const &amp;b) { cout &lt;&lt; "AB" &lt;&lt; endl; }
  };
  
  class B : public A {
    public:
      void operator=(A const &amp;a) { cout &lt;&lt; "BA" &lt;&lt; endl; }
  };
  
  int main() {
    B b1;
    B b2;
    b1.operator=(b2);
    return 0;
  }
  </code></pre>
  
  <p>This program prints</p>
  
  <pre><code>AA
  </code></pre>
  
  <p>The only difference between these two programs is function name: test and operator=.
  I don't understand why C++ behaves like this.
  Is there anything I'm missing?</p>
  
  <p>I'm compling this program with g++ version of 4.2.1 under mac OS X 8.2</p>
  
  <p>Thanks!</p>