title: How does operator== work outside of a class in C++
body: <p>I was reading <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md" rel="nofollow noreferrer">CppCoreGuidelines</a> and I stumbled upon something I didn't know before. Basically, it says that I can overload i.e. <code>operator==</code> outside of a class and it will somehow work for comparing two objects of this class. I did that:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class foo
  {
  public:
      int member;
      foo() : member(0) {}
  };
  
  bool operator==(const foo&amp; lhs, const foo&amp; rhs)
  {
      return lhs.member == rhs.member;
  }
  
  int main()
  {
      foo c1;
      foo c2;
      if (c1 == c2)
      {
          std::cout &lt;&lt; "Even" &lt;&lt; '\n';
      }   
  }
  </code></pre>
  
  <p>And well, it actually works. So I started to look for the explanation on the internet. Cuz I can imagine that if I try to compare an object, the compiler searches for the <code>operator==</code> in this class methods, that is understandable for me. But here I have a function which I guess could be defined in a different file, it's completely independent of the <code>foo</code> class (only the parameters it takes) and yet it is being recognized as the one to be used for comparison.</p>
  
  <p>So now my question is: How does the compiler or I guess more specifically the linker find this function and it uses it to compare those two objects?</p>