关于C ++中的全局命名空间 [英] Regarding the global namespace in C++

查看:441
本文介绍了关于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.

这篇关于关于C ++中的全局命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆