Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Stylistic choices

Name: Anonymous 2011-12-13 3:18

I tend to prefer to use namespaces filled with constants, rather than using enums in most cases. For example using

namespace Element {
const uint8_t No_Element = 0;
const uint8_t Air = 1;
const uint8_t Water = 2;
const uint8_t Earth = 3;
const uint8_t Fire = 4;
};


instead of


enum Element {
No_Element, Air, Water, Earth, Fire
};


Is this bad practice?

Name: Anonymous 2011-12-13 3:31

Yes, use C++11 typesafe enum classes.

Name: 5 2011-12-13 3:36

example
a.cpp:
enum class Element {
    No_Element,
    Air,
    Water,
    Earth,
    Fire
};

int main() {
    Element x = Element::Air;
    int i = x;
    x = 3;
    return 0;
}



$ gcc -std=c++0x a.cpp
a.cpp: In function ‘int main()’:
a.cpp:11:10: error: cannot convert ‘Element’ to ‘int’ in initialization
a.cpp:12:6: error: cannot convert ‘int’ to ‘Element’ in assignment

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List