The code you see is a specialized method for declaring variables in if
. You usually see something like this:
if (T* ptr = function()) { /* ptr is non-NULL, do something with it here */ } else { /* ptr is NULL, and moreover is out of scope and can't be used here. */ }
A particularly common case is to use dynamic_cast
here:
if (Derived* dPtr = dynamic_cast<Derived*>(basePtr)) { } else { }
What happens in your case is that you declare a double
inside the if
. C ++ automatically interprets any non-zero value as true
and any zero value as false
. This code means "declare d
and set it to fd()
. If it is nonzero, execute the if
."
However, this is a very bad idea, because double
are subject to all kinds of rounding errors, which in most cases prevent them from being 0. This code will almost certainly execute the body of the if
if the function
does not behave very well.
Hope this helps!
source share