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;    // 返回正常退出
    }
  • 相关阅读:
    python3-使用进程方式进行并发请求
    matplotlib--添加图例和注解
    matplotlib修改坐标轴刻度值,刻度个数
    matplotlib(一)
    python-jit(提高代码运行速度)
    pandans处理Excel
    base64编解码实现
    wireshark使用
    python 教程
    linux命令之 top, free,ps
  • 原文地址:https://www.cnblogs.com/yifengs/p/15086403.html
Copyright © 2011-2022 走看看