Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which operator overloading has been used for ifstream object to evalute to boolean

Tags:

c++

visual-c++

I am new to C++. recently I come across the following code

ifstream in("somefile");

if(in){
    //read the file....
}

I am wondering which operator overloading the ifstream might have used for the in object to automatically evaluate to boolean in if condition. I tried but couldnt find a clue. please help me. thank in advance

like image 359
Vineel Kumar Reddy Avatar asked Dec 28 '25 03:12

Vineel Kumar Reddy


1 Answers

It's actually operator void *.

It's overridden to return a non-zero pointer if the stream is valid, and a NULL pointer otherwise. The pointer it returns is meaningless and should not be dereferenced, it's only there to be evaluated in a boolean context.

like image 97
Frédéric Hamidi Avatar answered Dec 30 '25 17:12

Frédéric Hamidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!