Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Three things are being declared here: an anonymous enumerated type is declared, <code>ShapeType</code> is being declared a typedef for that anonymous enumeration, and the three names <code>kCircle</code>, <code>kRectangle</code>, and <code>kOblateSpheroid</code> are being declared as integral constants.</p> <p>Let's break that down. In the simplest case, an enumeration can be declared as</p> <pre><code>enum tagname { ... }; </code></pre> <p>This declares an enumeration with the tag <code>tagname</code>. In C and Objective-C (but <em>not</em> C++), any references to this <em>must</em> be preceded with the <code>enum</code> keyword. For example:</p> <pre><code>enum tagname x; // declare x of type 'enum tagname' tagname x; // ERROR in C/Objective-C, OK in C++ </code></pre> <p>In order to avoid having to use the <code>enum</code> keyword everywhere, a typedef can be created:</p> <pre><code>enum tagname { ... }; typedef enum tagname tagname; // declare 'tagname' as a typedef for 'enum tagname' </code></pre> <p>This can be simplified into one line:</p> <pre><code>typedef enum tagname { ... } tagname; // declare both 'enum tagname' and 'tagname' </code></pre> <p>And finally, if we don't need to be able to use <code>enum tagname</code> with the <code>enum</code> keyword, we can make the <code>enum</code> anonymous and only declare it with the typedef name:</p> <pre><code>typedef enum { ... } tagname; </code></pre> <p>Now, in this case, we're declaring <code>ShapeType</code> to be a typedef'ed name of an anonymous enumeration. <code>ShapeType</code> is really just an integral type, and should only be used to declare variables which hold one of the values listed in the declaration (that is, one of <code>kCircle</code>, <code>kRectangle</code>, and <code>kOblateSpheroid</code>). You can assign a <code>ShapeType</code> variable another value by casting, though, so you have to be careful when reading enum values.</p> <p>Finally, <code>kCircle</code>, <code>kRectangle</code>, and <code>kOblateSpheroid</code> are declared as integral constants in the global namespace. Since no specific values were specified, they get assigned to consecutive integers starting with 0, so <code>kCircle</code> is 0, <code>kRectangle</code> is 1, and <code>kOblateSpheroid</code> is 2.</p>
 

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