C++, reading a file using ifstream

c++, ifstream

Solution

The `ifstream` class has an `operator void *()` (or `operator bool()` in C++11). This is what is called when you test `(fin == NULL)`.

Testing `fin == NULL` should be exactly the same as testing `fin.fail()`.

Problem

``` class Person { private: string firstName; string lastName; public: Person() {} Person(ifstream &fin) { fin >> firstName >> lastName; } void print() { cout << firstName << " " << lastName << endl; } }; int main() { vector<Person> v; ifstream fin("people.txt"); while (true) { Person p(fin); if (fin == NULL) { break; } v.push_back(p); } for (size_t i = 0; i < v.size(); i++) { v[i].print(); } fin.close(); return 0; } ``` Please can you explain me, how following code snippet works? if (fin == NULL) { break; } fin is a object on stack, not a pointer so it can not become NULL. I was unable to find overloaded operator== function in ifstream class. So I can not understand how this snippet works.

Original source