1. What is the function of the void * cast operator and how does it work here.
It looks something like this:
operator void* () const { return fail() ? 0 : this; }
The question is why operator bool is not used here? The answer is: because it allows you to use incorrect conversions that can hide errors. The above example is a safe bool identifier .
However, this implementation is actually out of date. There are better implementations of this idiom; article explains them.
2.how is a non-null pointer converted to true and null to false
This is exactly how C ++ works: any non-zero pointer is considered equivalent to true in the conditional expression. So why does C ++ refer to operator void* here in the first place?
Essentially, when C ++ sees an object of an unexpected type, it tries to apply one implicit conversion that will make the type of the object valid in this context. Therefore, the compiler checks all available implicit conversions and looks to see if the resulting type is acceptable in this context.
This happens with her: the compiler sees while (cin >> grade) . He knows that basic_istream not valid in the context of the while condition. Thus, it discovers that operator void* exists, and a void* is valid in this context, therefore C ++ applies this transformation.
source share