Creating a C++ namespace in header and source (cpp)

前端 未结 8 1564
猫巷女王i
猫巷女王i 2020-12-23 00:22

Is there any difference between wrapping both header and cpp file contents in a namespace or wrapping just the header contents and then doing using namespace

相关标签:
8条回答
  • 2020-12-23 01:06

    I think right thing to do here is to use namespace for scoping.

    namespace catagory
    {
        enum status
        {
          none,
          active,
          paused
        }
    };
    
    void func()
    {
        catagory::status status;
        status = category::active;
    }
    
    0 讨论(0)
  • 2020-12-23 01:11

    There's no performance penalties, since the resulting could would be the same, but putting your Foo into namespace implicitly introduces ambiguity in case you have Foos in different namespaces. You can get your code fubar, indeed. I'd recommend avoiding using using for this purpose.

    And you have a stray { after using namespace ;-)

    0 讨论(0)
提交回复
热议问题