开发者

!= operator and file streams

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    ifstream file0( "file0.txt" );
    ifstream file1( "file1.txt" );

    if (file0 != file1) {
        cout << "What is being compared?" << endl;
    }

} 

If the above code, what is being compared in the conditional? I believe it is 开发者_Go百科pointer values but I am unable to find supporting evidence.

Thanks!


When doing a comparison on an ifstream the operator void* will be called. If you are using visual studio you can see this if you choose to see the disassembly of the code.

The operator can be found here. As you can see mentioned:

The pointer returned is not intended to be referenced, it just indicates success when none of the error flags are set.

So if both ifstreams fail, they will be equal. If they succeed (although I am not sure where the pointer value comes from) they will not be equal [this has been tested on VS].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜