zoukankan      html  css  js  c++  java
  • 二义性

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    namespace LOL
    {
        int swkId = 1;
    }
    
    void test01()
    {
        int swkId = 2;
    
        using LOL::swkId;        //写了using声明后,下面这行代码说明以后看到的swkId是LOL命名空间下的
                                //但是编译器又有就近原则,就产生了二义性
        cout << swkId << endl;    //二义性报错
    }
    int main()
    {
        test01();
        system("Pause");        //阻塞功能
        return EXIT_SUCCESS;    // 返回正常退出
    }

    结果:

    避免二义性

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    namespace LOL
    {
        int swkId = 1;
    }
    
    namespace King
    {
        int swkId = 3;
    }
    
    void test01()
    {
        int swkId = 2;
        using namespace LOL;        //写了using声明后,只是打开了命名空间 没有使用,不会产出二义性
                                
        cout << swkId << endl;        //就近原则 输出: 2
    }
    int main()
    {
        test01();
        system("Pause");        //阻塞功能
        return EXIT_SUCCESS;    // 返回正常退出
    }

    使用多个命名空间下字段

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    namespace LOL
    {
        int swkId = 1;
    }
    
    namespace King
    {
        int swkId = 3;
    }
    
    void test01()
    {
        using namespace LOL;        //写了using声明后,只是打开了命名空间 没有使用,不会产出二义性
        using namespace King;        //多个命名空间下相同字段名,产出二义性
        //cout << swkId << endl;        //报错
        cout << LOL::swkId << endl;        //使用指定命名空间下的变量
    }
    int main()
    {
        test01();
        system("Pause");        //阻塞功能
        return EXIT_SUCCESS;    // 返回正常退出
    }
  • 相关阅读:
    算法 在连续线性空间里查找
    OSI网络模型和网络连接设备
    CPU 架构SMP/NUMA,调优
    cluster集群基本概念
    protocol buffers 使用方法
    Qt在window下的环境变量PATH的配置
    mysql connector c++ 1.1 API初步体验
    mysql connector c++ 1.1 安装
    python知识点总结01(不定时更新)
    ant-design表单自定义验证
  • 原文地址:https://www.cnblogs.com/yifengs/p/15086403.html
Copyright © 2011-2022 走看看