title: Why can an ifstream object be compared to 0?
body: <p>I'm trying to get back into learning C++ after having first learned the basics about 20 years ago but then setting it aside.  I started by picking up a book I had learned from, back then.  Here's one of his examples (I've already figured out that it has some obsolete syntax):</p>
  
  <pre><code>#include &lt;fstream.h&gt;
  void main() {
      ifstream infile("iocopy.cpp");
      if (!infile) cerr &lt;&lt; "couldn't open iopcopy.cpp" &lt;&lt; endl;
      ... other junk
  }
  </code></pre>
  
  <p>His explanation of the <code>!</code> operator on <code>infile</code> was that it "checks to see if the object is nonzero", which was confusing to me (what does it mean for an object to be zero?).  So I decided to try it myself:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  void main () {
      std::ifstream f ("something");
  //  if (f == 0) std::cout &lt;&lt; "Couldn't open file" &lt;&lt; std::endl;
      if (!f) std::cout &lt;&lt; "Couldn't open file" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>This works as expected, displaying a message if the file doesn't exist and nothing if it does.  It also works if I uncomment the line with <code>(f == 0)</code> and comment out the following line.</p>
  
  <p>I think I figured out the <code>!</code> operator here; I found <code>operator!</code> in one of the header files defined to return a <code>fail</code> field.  I'm confused about why the first example <code>(f == 0)</code> works, though, since I didn't see any <code>operator==</code> that would seem to apply.  </p>
  
  <p>So what's going on?  Can any variable of any class type be compared to 0?  Is there something special about <code>ifstream</code> that variables of that type are essentially pointers (that could be compared to null or 0)?  Or is there an <code>operator==</code> redefinition that I missed somewhere?  I'm more used to languages like Java where it's impossible for a constructor to return a null value, so I'm confused.</p>