Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Several reasons:</p> <p><strong>Reason 1: Flexibility:</strong></p> <pre><code>enum lickahoctor { yes = 0, no = 1, maybe = 2 }; </code></pre> <p>declares an enumeration. You can use the values <code>yes</code>, <code>no</code> and <code>maybe</code> anywhere and assign them to any integral type. You can also use this as a type, by writing</p> <pre><code>enum lickahoctor myVar = yes; </code></pre> <p>This makes it nice because if a function takes a parameter with the type enum lickahoctor you'll know that you can assign <code>yes</code>, <code>no</code> or <code>maybe</code> to it. Also, the debugger will know, so it'll display the symbolic name instead of the numerical value. Trouble is, the compiler will only let you assign values you've defined in <code>enum lickahoctor</code> to <code>myVar</code>. If you for example want to define a few flags in the base class, then add a few more flags in the subclass, you can't do it this way.</p> <p>If you use an int instead, you don't have that problem. So you want to use some sort of int, so you can assign arbitrary constants.</p> <p><strong>Reason 2: Binary compatibility:</strong></p> <p>The compiler chooses a nice size that fits all the constants you've defined in an enum. There's no guarantee what you will get. So if you write a struct containing such a variable directly to a file, there is no guarantee that it will still be the same size when you read it back in with the next version of your app (according to the C standard, at least -- it's not quite that bleak in practice).</p> <p>If you use some kind of int instead, the platform usually guarantees a particular size for that number. Especially if you use one of the types guaranteed to be a particular size, like <code>int32_t</code>/<code>uint32_t</code>.</p> <p><strong>Reason 3: Readability and self-documentation</strong></p> <p>When you declare myVar above, it's immediately obvious what values you can put in it. If you just use an int, or an <code>uint32_t</code>, it isn't. So what you do is you use</p> <pre><code>enum { yes, no, maybe }; typedef uint32_t lickahoctor; </code></pre> <p>to define a nice name for the integer somewhere near the constants that will remind people that a variable of this type can hold this value. But you still get the benefit of a predictable, fixed size and the ability to define additional values in subclasses, if needed.</p> <p><strong>Reason 4: Support for bitfields</strong></p> <p>enum-typed variables only support assigning exactly one value from their options. So if you're trying to implement a bit field, you can't type it as a bitfield. Furthermore, you need to use unsigned variables to avoid sign extension from screwing you over.</p>
    singulars
    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.
 

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