zoukankan      html  css  js  c++  java
  • C++学习笔记之 命名空间

    命名空间

    #include <iostream>
    #include <cstdio>
    
    // using namespace std::cout; // 使用std中的cout
    using namespace std; // 使用std中的所有定义
    
    namespace A
    {
        int a = 0;
    }
    
    namespace B
    {
        int a = 10;
    }
    
    // 用途:解决名称冲突
    void test1(void)
    {
        cout << "test1:" << endl;
        cout << "A::a = " << A::a << "
    B::a = " << B::a << endl;
    }
    
    // 命名空间必须声明在全局作用域下
    void test(void)
    {
        // 局部作用域下不能定义命名空间
        //namespace _test
        //{
        //    int a = 0;
        //}
    }
    
    // 命名空间可以嵌套命名空间
    namespace C
    {
        int int_a = 0;
        namespace D
        {
            int int_a = 1;
        }
    }
    
    void test2(void)
    {
        cout << "
    test2:" << endl;
        printf("C::int_a = %d
    C::D::int_a = %d
    ",C::int_a,C::D::int_a);
    }
    
    // 命名空间是开放的,可以随时添加新成员
    // 同名的命名空间做 合并操作
    namespace C
    {
        char char_a = 'C';
        void hello();
    }
    
    void C::hello()
    {
        printf("Hello
    ");
    }
    
    void test3(void)
    {
        cout << "
    test3:" << endl;
        cout << "C::char_a = " << C::char_a << endl;
        C::hello();
    }
    
    // 命名空间可以匿名
    namespace
    {
        int quanju = 0; // 命名空间是匿名时 变量前隐式添加了关键字static,相当于全局变量
    }
    
    void test4(void)
    {
        cout << "
    test4:" << endl;
        cout << quanju << endl;
    }
    
    // 命名空间可以起别名
    namespace long_long_name
    {
        int a = 0;
    }
    
    void test5(void)
    {
        namespace short_name = long_long_name;
        cout << "
    test5:" << endl;
        cout << short_name::a << endl;
    }
    
    int main(int argc, char *argv[])
    {
        test1();
        test2();
        test3();
        test4();
        test5();
    
        return 0;
    }
    

    using指令

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    // 1. using声明
    
    namespace C
    {
        char name[] = "C语言";
    }
    
    namespace cpp
    {
        char name[] = "C++";
    }
    
    void test1()
    {
        char name[] = "C++";
    
        // using C::name; // 提前告诉编译器,name用C里的
        // using声明原则与就近原则同时出现,无法使用,二义性出现
        // 无法解决,尽量避免
        cout << name << endl;
    }
    
    // 2. using编译指令
    
    void test2()
    {
        int name = 1;
    
        using namespace C; // 打开C命名空间
        using namespace cpp;
    
        cout << name << endl; // 和就近原则同时出现,优先使用就近原则
    }
    
    int main(int argc,char *argv[])
    {
        test1();
        test2();
    
        return EXIT_SUCCESS;
    }
    
    C++
    1
    
  • 相关阅读:
    Mac 卸载MySql的方法
    关于mysql的wait_timeout参数 设置不生效的问题
    linux下利用nohup后台运行jar文件包程序
    MySql创建视图
    spring mvc获取header
    Spring Data Jpa 查询返回自定义对象
    Caused by: org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must end with the ';' delimiter.
    eclipse Reference 功能之——项目之间的引用
    Mac 在启动eclipse时 Failed to load JavaHL Library解决方法
    MySQL Workbench update语句错误Error Code: 1175.
  • 原文地址:https://www.cnblogs.com/zhujiangyu/p/13904279.html
Copyright © 2011-2022 走看看