namespaces for enum types - best practices

前端 未结 8 1914
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 06:34

Often, one needs several enumerated types together. Sometimes, one has a name clash. Two solutions to this come to mind: use a namespace, or use \'larger\' enum element na

8条回答
  •  無奈伤痛
    2020-12-04 06:52

    I would definitely avoid using a class for this; use a namespace instead. The question boils down to whether to use a namespace or to use unique ids for the enum values. Personally, I'd use a namespace so that my ids could be shorter and hopefully more self-explanatory. Then application code could use a 'using namespace' directive and make everything more readable.

    From your example above:

    using namespace Colors;
    
    void setPenColor( const e c ) {
        switch (c) {
            default: assert(false);
            break; case cRed: //...
            break; case cBlue: //...
            //...
        }
    }
    

提交回复
热议问题