zoukankan      html  css  js  c++  java
  • c++ namespace的使用

    ** namespace:命名空间就是为解决C++中的变量、函数的命名冲突而服务的。

    ** namespace定义的格式基本格式是:

      namespace identifier
      {
          entities;
      }

      举个例子,
      namespace exp
      {
          int a,b;
      }

      为了在namespace外使用namespace内的变量,使用::操作符,如下

      exp::a
      exp::b

      使用namespace可以有效地避免重定义,

     1 #include <iostream>
     2 using namespace std;
     3 
     4 namespace first
     5 {
     6   int var = 5;
     7 }
     8 
     9 namespace second
    10 {
    11   double var = 3.1416;
    12 }
    13 
    14 int main () {
    15   cout << first::var << endl;
    16   cout << second::var << endl;
    17   return 0;
    18 }

      结果是:
      5
      3.1416

      两个全局变量都是名字都是var,但是他们不在同一个namespace中所以没有冲突。

    ** using 关键字

      关键字using可以帮助从namespace中引入名字到当前的声明区域,

     1 #include <iostream>
     2 using namespace std;
     3 
     4 namespace first
     5 {
     6   int x = 5;
     7   int y = 10;
     8 }
     9 
    10 namespace second
    11 {
    12   double x = 3.1416;
    13   double y = 2.7183;
    14 }
    15 
    16 int main () {
    17   using first::x;
    18   using second::y;
    19   cout << x << endl;
    20   cout << y << endl;
    21   cout << first::y << endl;
    22   cout << second::x << endl;
    23   return 0;
    24 }
    25 输出是
    26 5
    27 2.7183
    28 10
    29 3.1416
    30 就如我们所指定的第一个x是first::x,y是second.y

      using也可以导入整个的namespace,

     1 #include <iostream>
     2 using namespace std;
     3 
     4 namespace first
     5 {
     6   int x = 5;
     7   int y = 10;
     8 }
     9 
    10 namespace second
    11 {
    12   double x = 3.1416;
    13   double y = 2.7183;
    14 }
    15 
    16 int main () {
    17   using namespace first;
    18   cout << x << endl;
    19   cout << y << endl;
    20   cout << second::x << endl;
    21   cout << second::y << endl;
    22   return 0;
    23 }
    24 输出是
    25 5
    26 10
    27 3.1416
    28 2.7183

    ** namespace也支持嵌套

     1 #include <iostream>
     2 
     3 namespace first
     4 {
     5     int a=10;
     6     int b=20;
     7 
     8     namespace second
     9     {  
    10         double a=1.02;
    11         double b=5.002;
    12         void hello();
    13     }  
    14 
    15     void second::hello()
    16     {  
    17     std::cout <<"hello world"<<std::endl;
    18     }
    19 }
    20 
    21 int main()
    22 {
    23     using namespace first;
    24 
    25     std::cout<<second::a<<std::endl;
    26     second::hello();
    27 }
    28 输出是
    29 1.02
    30 hello world

    在namespace first中嵌套了namespace second,seond并不能直接使用,需要first来间接的使用。

    ** namespace 可以取别名

      在对一些名字比较长的namespace使用别名的话,是一件很惬意的事。但是与using相同,最好避免在头文件使用namespace的别名(f比first更容易产生冲突)。
    namespace f = first;

  • 相关阅读:
    Spring框架学习09——基于AspectJ的AOP开发
    Spring框架学习08——自动代理方式实现AOP
    Spring框架学习07——基于传统代理类的AOP实现
    Spring框架学习06——AOP底层实现原理
    Spring框架学习05——AOP相关术语详解
    SpringMVC框架09——@ResponseBody的用法详解
    Spring框架学习04——复杂类型的属性注入
    Spring框架学习03——Spring Bean 的详解
    Spring框架学习01——使用IDEA开发Spring程序
    sqlserver 迁移
  • 原文地址:https://www.cnblogs.com/chencesc/p/4287368.html
Copyright © 2011-2022 走看看