Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining double exclamation?

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.

like image 577
Cemre Mengü Avatar asked Jul 07 '12 12:07

Cemre Mengü


People also ask

What does it mean when someone uses 2 exclamation marks?

It is used to end a rhetorical question or a simultaneous question and exclamation. Some writers, then, began using multiple exclamation points as a logical outgrowth of the interbang and single exclamation mark to add even more emphasis to words, phrases, and sentences.

Can there be 2 exclamation marks?

Use the number of exclamation points that's in your heart. Language is supposed to help you communicate what you mean, so if you need two exclamation points for an extra-emphatic opinion and 27 for an announcement to your brother about your promotion, go for it.

Is double exclamation rude?

No! An exclamation point (or mark) is used after an interjection or to show emphasis or strong emotion.


1 Answers

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.

like image 198
Jeffery Thomas Avatar answered Oct 14 '22 20:10

Jeffery Thomas