不然608 2022-12-23 16:27 采纳率: 71.4%
浏览 12
已结题

关于命名空间using

using命令这一行,放在printf语句上面任何位置是不是都不影响输出的结果,求解

img

  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-12-23 16:48
    关注
    
    using Directive
    The using-directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. In contrast to a using declaration, which allows an individual name to be used without qualification, the using directive allows all the names in a namespace to be used without qualification. See using Declaration for more information. 
    
    Syntax
    
    using-directive :
    
    using namespace ::opt nested-name-specifieropt namespace-name
    
    The intent of the using-directive is to allow unique, descriptive names to be used when declaring functions and variables, without requiring the complete name every time access to the functions or variables is needed. Of course, the complete, qualified name can still be used to retain clarity.
    
    The unqualified names can be used from the point of the using directive on. If a namespace is extended after a using-directive is given, the additional members of the namespace can be used, without qualification, after the extended-namespace-definition. For example:
    
    namespace M
    {
        int i;
    }
    
    using namespace M;
    
    namespace N
    {
        int j;
        double f() { return M::d; }        // error: M::d does not yet exist
    }
    
    namespace M        // namespace extension
    {
        double d;
    }
                       // now M::d can be used
    
    It is possible for a using-directive to introduce conflicting names when used in another namespace. For example:
    
    namespace M
    {
        int i;
    }
    
    namespace N
    {
        int i;
        using namespace M;    // no error here
    }
        .
        .
        .
    void f()
    {
        using namespace N;
        i = 7;                // error: ambiguous: M::i or N::i?
    }
    
    In this example, bringing M::i into namespace N does not hide the declaration of N::i, but instead creates an ambiguity when N::i is used. In this manner, the using-directive can easily introduce unintended ambiguities. Consider the following code fragment:
    
    namespace D
    {
        int d1;
        void f(int);
        void f(char);
    }
    
    using namespace D;
    
    int d1;             // no conflict with D::d1
    
    namespace E
    {
        int e;
        void f(int);
    }
    
    namespace D         // namespace extension
    {
        int d2;
        using namespace E;
        void f(int);
    }
    
    void f()
    {
        d1++;           // error: ambiguous: ::d1 or D::d1?
        ::d1++;         // ok
        D::d1++;        // ok
        d2++;           // ok: D::d2
        e++;            // ok: E::e
        f(1);           // error: ambiguous: D::f(int) or E::f(int)?
        f('a');         // ok D::f(char)
    }
    
    When a variable is referenced after a using-directive, the local variable of the same name takes precedence over the one declared in the specified namespace. For example:
    
    namespace N {
       int data = 4;
    }
    
    void f(bool flag) {
       int data = 0;
    
       if (flag) {
          using namespace N;
       
          prinf(“data=%d\n”, data);
       }
    }
    
    void main() {
       f(true);
    }
    
    In the above code, the variable data referenced in the printf statement is the local variable initialized to 0, instead of the variable initialized in namespace N. The output is data=0 instead of data=4.
    
    In the presence of namespace using-directives, the way qualified names are looked up is shown in the following example:
    
    namespace A {
       int flag = 0;
    }
    
    namespace B {
       using namespace A;
    }
    
    namespace C {
       using namespace A;
       using namespace B;
    }
    
    void main() {
       printf(“C::flag = %d\n”, C::flag);
    }
    
    The qualified name (C::flag) is resolved to (A::flag) due to the namespace using-directives in namespace C.
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月29日
  • 已采纳回答 1月21日
  • 创建了问题 12月23日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价