I understand what a double exclamation mark does (or I think I understand) but I am not sure how it is defined on a random object. For example in the code snippet below:
Assignment *a;
if (!getAssignment(query, a))
return false;
hasSolution = !!a;
if (!a)
return true;
How do I know what value will the double exclamation mark result in ? In other words does it always convert to true ? false ? or can you define a behavior for it such as executing a method to determine the result (how does the object know how to act in this situation) ? I am bit confused about this piece of code due to all these exclamation stuff going on.. Any explanation is appreciated.
Hope I was clear and thanks.
a
is a pointer. In C++, nullptr
is defined to be an invalid pointer. !pointer
turns a nullptr
pointer into true
and a non nullptr
pointer into false
. !boolean
turns true
into false
and false
into true
. It will always work.
!(!a)
is a useful way to think of it.