What's the proper way to share an enum across multiple files?

后端 未结 2 653
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-09 22:47

I would like to use the same enum in both the client and server parts of my current (C++) project, but am unsure of the proper way to do this. I could easily just write the enu

2条回答
  •  佛祖请我去吃肉
    2021-02-09 23:07

    Putting it in a header file seems reasonable, and wrapping it in a namespace can be useful too. Something i find myself doing with enums is

    contents of whatevsenum.hpp:

    namespace whatevs
    {
        enum Enum
        {
            FOO = 0,
            BAR,
            BLARGH,
            MEH,
            SIZE
        };
    } // namespace whatevs
    

    and then whenever i need to use it in a header/source file elsewhere:

    #include "whatevsenum.hpp"
    
    void do_something_with(whatevs::Enum value)
    {
        // do stuff
    }
    
    ...
    
    do_something_with(whatevs::FOO);
    

提交回复
热议问题