Namespaces qualified with :: in C++

前端 未结 5 1951
余生分开走
余生分开走 2021-01-05 02:46

What does it mean if namespace in C++ is qualified with ::? For example ::testing::Test.

5条回答
  •  梦毁少年i
    2021-01-05 02:56

    I if you precede a variable name with ::, it resolves the variable to the global scope. In this way, you can have both a local variable testing and global variable testing and differentiate between them.

    #include 
    
    using namespace std;
    int testing = 37;
    
    int main()
    {
       int testing = 42;
    
       cout << ::testing << ' ' << testing << endl;
    }
    

    Output will be 37 42

提交回复
热议问题