title: Forcing improper enum values?
body: <p>I came across some unusual (to me at least...) behavior with C++'s enums.  I've tried the following in Visual Studio 2008 and g++ version 4.4.3</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  enum testEnum
  {
      // no zero enum
      one = 1,
      two = 2,
      three = 3
  };
  
  int main(int argc, char *argv[])
  {
      testEnum e;  // undefined value (may be zero, but thats just luck)
      cout &lt;&lt; "Uninitialized enum e = " &lt;&lt; e &lt;&lt; endl;
  
      /*
          testEnum e2(0);  // error converting from int to enum
      */
  
      testEnum e3(testEnum(0)); // forces zero !?!!?!?
      cout &lt;&lt; "zero enum e3 = " &lt;&lt; e3 &lt;&lt; endl;  // prints '0'
  
      testEnum e4(testEnum(9999)); // forces 9999 ?!?!?!?!
      cout &lt;&lt; "9999 enum e4 = " &lt;&lt; e4 &lt;&lt; endl; // prints '9999'
  
      return 0;
  }
  </code></pre>
  
  <p>The undefined value for e is as I expected, and I understand why you can't convert from an int to an enum (but you can go the other way).</p>
  
  <p>I'm curious as to how the last two enums (e3 and e4) are allowed to compile and attain any value you want to  give them.</p>
  
  <p>Also, I found that this:</p>
  
  <pre><code>testEnum e();
  </code></pre>
  
  <p>Compiled in both studio and linux, and cout-ing in linux yielded '1' but in studio I got a linker error:</p>
  
  <pre><code>main.obj : error LNK2001: unresolved external symbol "enum testEnum __cdecl e2(void)"
  </code></pre>
  
  <p>In studio, I could do:</p>
  
  <pre><code>testEnum e = testEnum();
  </code></pre>
  
  <p>But cout-ing it yielded '0' not '1'</p>
  
  <p>So my main question is how you can ram any value down an enum's throat like e3 and e4 in the above example.  And if this is implementation dependent or not.</p>