关于C ++中的全局命名空间
在C ++中,我们应该使用 ::
?
In C++, should we be prepending stuff in the global namespace with ::
?
当使用WinAPI,它在C,应该我 :: HANDLE
而不是 HANDLE
和 :: LoadLibrary
而不是 LoadLibrary
? C ++是什么说的?
For example, when using WinAPI, which is in C, should I do ::HANDLE
instead of HANDLE
, and ::LoadLibrary
instead of LoadLibrary
? What does C++ say about this? Is it generally a good idea, factoring in issues like readability and maintainability?
由于命名空间在C中不存在,所以don是一个好主意, 't use :: HANDLE访问HANDLE类型。
As namespaces don't exists in C, don't use ::HANDLE to access HANDLE type.
使用prepending :: for全局命名空间是一个好主意,为了可读性,你知道你想要访问的类型是从全局命名空间。
Using the prepending :: for global namespace is a good idea for readability, you know the type you want to access is from global namespace.
此外,如果你在一个嵌套的命名空间并声明自己的HANDLE类型(例如),那么编译器将使用这一个而不是windows。 h一个!
Moreover, if you are in a nested namespace and declare your own HANDLE type (for example), then the compiler will use this one instead of windows.h one!
因此,在嵌套命名空间中工作时,总是喜欢使用:: before names。
Thus, always prefer using :: before names when working in nested namespace.