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

前端 未结 8 1582
猫巷女王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 00:49

    If you're attempting to use variables from one to the other, then I'd recommend externalizing them, then initializing them in the source file like so:

    // [.hh]
    namespace example
    {
       extern int a, b, c;
    }
    // [.cc]
    // Include your header, then init the vars:
    namespace example
    {
       int a, b, c;
    }
    // Then in the function below, you can init them as what you want: 
    void reference
    {
        example::a = 0;
    }
    

提交回复
热议问题