加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C使用命名空间语句

发布时间:2020-12-16 03:25:59 所属栏目:百科 来源:网络整理
导读:namespace MyNamespace{ static void foo1() { }}using namespace MyNamespace;class MyClass{ void foo2() { ::foo1(); } }; 范围解析操作::表示在全局命名空间中使用方法.在这里我们可以使用:: foo1().这意味着方法foo1()在全局命名空间中,对吗? 我的问题
namespace MyNamespace
{
    static void foo1()
    {

    }
}

using namespace MyNamespace;

class MyClass
{
    void foo2()
    {
        ::foo1();
    }    
};

范围解析操作::表示在全局命名空间中使用方法.在这里我们可以使用:: foo1().这意味着方法foo1()在全局命名空间中,对吗?

我的问题是,使用命名空间ANAMESPACE_NAME是否意味着我们将命名空间ANAMESPACE_NAME中的所有元素导入全局命名空间?

解决方法

C 2003标准的第3.4.3.4节有一个答案:

A name prefixed by the unary scope operator :: (5.1) is looked up in
global scope,in the translation unit where it is used. The name shall
be declared in global namespace scope or shall be a name whose
declaration is visible in global scope because of a using-directive

(3.4.3.2).

这一段在C 11 FDIS中几乎相同,因此这可能也适用于C 11.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读