Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two possible reasons:</p> <ol> <li><p><strong>Assign &amp; Check</strong></p> <p>The <code>=</code> operator (when not overriden) normally returns the value that it assigned. This is to allow statements such as <code>a=b=c=3</code>. In the context of your question, it also allows you to do something like this:</p> <pre><code>bool global;//a global variable //a function int foo(bool x){ //assign the value of x to global //if x is equal to true, return 4 if (global=x) return 4; //otherwise return 3 return 3; } </code></pre> <p>...which is equivalent to but shorter than:</p> <pre><code>bool global;//a global variable //a function int foo(bool x){ //assign the value of x to global global=x; //if x is equal to true, return 4 if (global==true) return 4; //otherwise return 3 return 3; } </code></pre> <p>Also, it should be noted (as stated by Billy ONeal in a comment below) that this can also work when the left-hand argument of the <code>=</code> operator is actually a class with a <a href="http://www.devx.com/tips/Tip/12459" rel="nofollow noreferrer">conversion operator</a> specified for a type which can be coerced (implicitly converted) to a bool. In other words, <code>(a=b)</code> will evaulate to <code>true</code> or <code>false</code> if a is of a type which can be coerced to a boolean value.</p> <p>So the following is a similar situation to the above, except the left-hand argument to <code>=</code> is an object and not a bool:</p> <pre><code>#include &lt;iostream&gt; using namespace std; class Foo { public: operator bool (){ return true; } Foo(){} }; int main(){ Foo a; Foo b; if (a=b) cout&lt;&lt;"true"; else cout&lt;&lt;"false"; } //output: true </code></pre> <p><em>Note:</em> At the time of this writing, the code formatting above is bugged. My code (check the source) actually features proper indenting, shift operators and line spacing. The <code>&amp;lt;</code>'s are supposed to be &lt;'s, and there aren't supposed to be enourmous gaps between each line.</p></li> <li><p><strong>Overridden <code>=</code> operator</strong></p> <p>Since C++ allows the overriding of operators, sometimes = will be overriden to do something other than what it does with primitive types. In these cases, the performing the <code>=</code> operation on an object could return a boolean (if that's how the <code>=</code> operator was overridden for that object type).</p> <p>So the following code would perform the <code>=</code> operation on <code>a</code> with <code>b</code> as an argument. Then it would conditionally execute some code depending on the return value of that operation:</p> <pre><code>if (a=b){ //execute some code } </code></pre> <p>Here, <code>a</code> would have to be an object and <code>b</code> would be of the correct type as defined by the overriding of the <code>=</code> operator for objects of <code>a</code>'s type. To learn more about operator overriding, see this wikipedia article which includes C++ examples: <a href="http://en.wikipedia.org/wiki/Operator_overloading" rel="nofollow noreferrer">Wikipedia article on operator overriding</a></p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload