Constanten vs Enumeration



  • Welchen IntegerTyp nimmt eine Enumeration an? Ist das abhängig vom Compiler (x32 oder x64). Hab's noch nicht ausprobiert.

    Vermute mal zum Aufzählen von Feldern/Linien/Reihen/... eines Schachprogrammes wären Enumerationen besser geeignet?!

    // 64 Konstanten vom Typ int
    
    const int SQUARE_A1 = 0;
    const int SQUARE_B1 = 1;
    const int SQUARE_C1 = 2;
    const int SQUARE_D1 = 3;
    // ...
    
    // oder als Enumeration
    
    enum
    {
      SQUARE_A1,
      SQUARE_B1,
      SQUARE_C1,
      SQUARE_D1,
      // ...
    };
    

    Gruss
    T.



  • Tomahawk schrieb:

    Welchen IntegerTyp nimmt eine Enumeration an? Ist das abhängig vom Compiler (x32 oder x64). Hab's noch nicht ausprobiert.

    Compilerabhängig.

    Tomahawk schrieb:

    Vermute mal zum Aufzählen von Feldern/Linien/Reihen/... eines Schachprogrammes wären Enumerationen besser geeignet?!

    Was spricht gegen zwei Enums (Reihe + Spalte) und eine Koordinaten-Struktur?



  • In meinen Büchern habe ich das so noch nie gelesen, wie es in den MSDN Libaries steht:

    By default, the underlying type of an enumeration is int. However, you can specify the type to be signed or unsigned forms of int, short, long, __int32, or __int64. You can also use char.

    public enum class day_char : char {sun, mon, tue, wed, thu, fri, sat};
    

    Bin jetzt etwas überrascht 😕



  • Das ist Bestandteil des zukünftigen C++ Standards (genannt C++0x). Das hat rein gar nichts mit C zu tun.



  • Jede enum ist ein eigener Integer-Typ. Der Standard spezifiziert allerdings eine Reihe von Kompatibilitätsvorschriften mit anderen Integertypen; ich denke, du suchst nach dieser hier:

    ISO/IEC 9899:1999 6.7.2.2 (4) schrieb:

    Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,108) but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until after the } that terminates the list of enumerator declarations.

    Fußnote 108) An implementation may delay the choice of which integer type until all enumeration constants have been seen.

    Allerdings (und etwas verwirrend) sind die Mitglieder einer enum vom Typ int, nicht vom Typen der enum selbst:

    ISO/IEC 9899:1999 6.7.2.2 (3) schrieb:

    The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. (...)

    Dementsprechend liefert

    #include <stdio.h>
    
    enum foo { bar };
    
    int main(void) {
      short x = 0;
    
      printf("%zu, %zu\n", sizeof(enum foo), sizeof(bar));
    
      return 0;
    }
    

    kompiliert mit

    gcc -fshort-enums enum.c
    

    die Ausgabe

    1, 4
    


  • Nachtrag: Die Ausgabe ist natürlich plattformspezifisch, aber ich denke, es ist klar, worauf ich hinaus will.



  • Janjan schrieb:

    Das ist Bestandteil des zukünftigen C++ Standards (genannt C++0x).

    Nö, das ist C++/CLI.



  • Michael E. schrieb:

    Janjan schrieb:

    Das ist Bestandteil des zukünftigen C++ Standards (genannt C++0x).

    Nö, das ist C++/CLI.

    Stimmt. Wäre nach dem "public" noch ein Doppeltpunkt und würde dies innerhalb einer Struktur/Klasse stehen, dann wäre es C++0x. 😉



  • Lässt sich auch nur mit der Option /clr compilieren.

    Danke für die Aufklärung.


Anmelden zum Antworten