编写“:: namespace :: identifier”和“和“namespace :: identifier”? [英] What is the difference between writing "::namespace::identifier" and "namespace::identifier"?

查看:161
本文介绍了编写“:: namespace :: identifier”和“和“namespace :: identifier”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中看到了两种方法。你能解释两者之间有什么区别吗?因为我认为它涉及命名空间查找方式由C ++做,你能提供一些信息,以及,或者一个链接到一个好的文档?谢谢。

I have seen both approaches in code. Could you explain what's the difference between the two? As I think it relates to the way namespace lookup is done by C++, could you provide some information on this as well, or perhaps a link to a good doc? Thanks.

推荐答案

这并不重要,至少不是大多数时候。

It doesn't really matter, at least not most of the time.

在格式 :: identifier1 :: identifier2 中,前面的冒号说明查看 identifier1 ,然后在该范围中查找 identifier2

In the format ::identifier1::identifier2, the preceding colon says to look at global scope for identifier1, and then look for identifier2 in that scope.

格式 identifier1 :: identifier2 ,我们改为查看 identifier1 当前范围。如果我们没有找到它,那么将搜索父作用域,依此类推,直到我们找到它。然后,我们在任何范围内搜索 identifier2

In the format identifier1::identifier2, we instead look at the current scope for identifier1. If we don't find it, then the parent scope is searched, and so on until we find it. We then search for identifier2 within whatever scope we just found.

范围,没关系。但是,当你在一个命名空间内工作,或者其他命名空间或类嵌套在其中的类时,这个故事会改变。

In the case where you're already at global scope, it won't matter. But that story changes when you're working within a namespace or classes that has other namespaces or classes nested within it.

这篇关于编写“:: namespace :: identifier”和“和“namespace :: identifier”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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