C ++使用名称空间语句 [英] C++ using namespace statement

查看:64
本文介绍了C ++使用名称空间语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace MyNamespace
{
    static void foo1()
    {

    }
}

using namespace MyNamespace;

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

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

The scope resolution operation :: means using method in the global namespace. Here we can use ::foo1(). This means method foo1() is in the global namespace, am I right?

我的问题是,using namespace ANAMESPACE_NAME是否意味着我们将所有来自命名空间ANAMESPACE_NAME的元素导入到全局命名空间中?

My question is, does using namespace ANAMESPACE_NAME mean we import all elements form the namespace ANAMESPACE_NAME into the global namespace?

推荐答案

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

Section 3.4.3.4 of the C++2003 standard has an answer:

在以下位置查找由一元作用域运算符::(5.1)开头的名称 全局范围,在使用它的翻译单位中.名称应 在全局名称空间范围中声明,或者应为其名称 由于使用指令,声明在全局范围内可见 (3.4.3.2).

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中也可能适用.

This paragraph is almost identical in the C++11 FDIS, so this probably also holds in C++11.

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

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