Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>case</code> statements in a <code>switch()</code> have the same semantics as <code>goto</code>: When you dispatch to a label in a <code>switch</code>-<code>case</code>, you effectively <code>goto</code> the <code>case</code> label. The reason for this is that cases in a <code>switch</code>-<code>case</code> aren't self-contained. You can fall-through from one case to another.</p> <p>In fact, <code>switch</code>-<code>case</code> is so <code>goto</code>-like that you can even write a monstrosity such as <a href="http://en.wikipedia.org/wiki/Duff%27s_device" rel="nofollow">Duff's Device</a>. Study that until you're properly horrified.</p> <p>In C++, locally defined objects come into scope at their point of definition, and go out of scope at the closing curly brace of the enclosing scope. A <code>break</code> or <code>continue</code> statement that exits that scope is guaranteed to cleanly handle objects going out of scope.</p> <p>For your <code>switch</code> statement, the enclosing scope is defined by the curly-braces after <code>switch()</code>. </p> <p>Now, in C++, it's illegal to use <code>goto</code> or something like it skip an object initialization. In your example, the <code>case</code> and <code>default</code> labels run afoul of that rule.</p> <p>Your fix avoids the issue by replacing object initialization with object assignment. That's one way to fix it. Another is to add an additional level of scope:</p> <pre><code> case WM_LBUTTONDOWN: { HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0)); break; } </code></pre> <p>This works because the curly braces provide an extra level of scope clearly defining when <code>hBrush</code>'s lifetime <em>ends</em>. </p> <p>In terms of technical detail: The statement that triggered the error relies on a <em>copy constructor</em>. The statement that did not relies on the <em>copy assignment</em> operator instead.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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