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;

时间: 2024-08-11 02:22:53

c++ namespace的使用的相关文章

There is no Action mapped for namespace [/user] and action name [user!add] associated with context p

使用struts2.3进行动态方法调用时出现: There is no Action mapped for namespace [/user] and action name [user!add] associated with context path错误,原因是 (1)DMI可能导致安全问题 (2)DMI与通配符方法功能有重叠,因此该版本Struts2默认关闭DMI,需要在struts.xml中加一句 <constant name="struts.enable.DynamicMetho

PHP之namespace小记

命名空间的使用 在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句.所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前. PHP 命名空间中的类名可以通过三种方式引用: 非限定名称,或不包含前缀的类名称. 限定名称,或包含前缀的名称. 完全限定名称,或者包含了全局前缀操作符的名称 file1.php <?php namespace Foo\Bar\subnamespace; const FOO = 1; function foo() {} class fo

namespace 命名空间

一.命名空间的定义 namespace 命名空间的名字 {类/变量/函数/模板/其他命名空间}; 命名空间空间可以定义在全局作用域和其他命名空间中,但不能定义在函数或类的内部. 二.命名空间的作用域 每一个命名空间都是一个作用域,定义在某个命名空间中的名字可以被该命名空间内的其他成员访问,也可以被这些成员的内嵌作用域中的任何单位访问. namespace nsp{ int num=10; class ab { public: int val(){return num;} }; } int val

A Universally Unique IDentifier (UUID) URN Namespace

w Network Working Group P. Leach Request for Comments: 4122 Microsoft Category: Standards Track M. Mealling Refactored Networks, LLC R. Salz DataPower Technology, Inc. July 2005 A Universally Unique IDentifier (UUID) URN Namespace Status of This Memo

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [product-save] associated with context path [/20161101-struts2-2].

运行:index.jsp---->input.jsp----->details.jsp,但是在input.jsp到details.jsp的时候报错误. 异常如下: 严重: Could not find action or result/There is no Action mapped for namespace [/] and action name [] associated with context path []. - [unknown location]at com.opensymp

php中的namespace 命名空间

名字解释: namespace(命名空间),命名空间是从php5.3开始支持的功能.作用主要有两个:1.可以避免类名取得过长.2.当在多个框架配合使用时,同名的类之间不会冲突. 命名空间,看名字就知道,目的就是为了命名的,不然咋不叫QQ空间 盗梦空间 七度空间.额 好像有奇怪的掺进来了. 下面是示例代码: 1 //在test1.php 中有一个类 叫做Person,它放在一个叫shop的namespace里. 2 <?php 3 namespace shop;//这个声明要放在php文件的最上面

C++ “sprintf_s”: 不是“`global namespace&#39;”的成员

最近从别的一个控制台项目复制了一些代码到dll项目中,编译后报错,提示:C++ “sprintf_s”: 不是“`global namespace'”的成员, 经检查,发现复制的代码中包含了 std::cout << "......" 猜测dll工程不支持这样的编译,删除后编译成功. http://stackoverflow.com/questions/3710168/how-do-i-build-notepad-with-visual-c-2010-express上有一个

vs2013c#测试using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1_CXY { class Program { stati

首先安装Unit Test Generator.方法为:工具->扩展和更新->联机->搜索“图标为装有蓝色液体的小试管.Unit Test Generator”, 编写代码,生成一个新的类,编写构造函数 与 add()函数.代码如下. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Co

关于Struts2中的错误:例如There is no Action mapped for namespace / and action name product-input.

            在配置struts.xml 文件时,会出现    There is no Action mapped for namespace / and action name ....    意思是没有product-input的映射文件或者是命名空间       出现这样的错误的原因后:     做如下检查:           1: 把struts.xml中的namespace="/"改成namespace=""或者去除,使用默认的命名空间. 2:

PHP中的命名空间(namespace)及其使用详解

PHP中的命名空间(namespace)及其使用详解 晶晶 2年前 (2014-01-02) 8495次浏览 PHP php自5.3.0开始,引入了一个namespace关键字以及__NAMESPACE__魔术常量(当然use关键字或use as嵌套语句也同时引入):那么什么是命名空间呢?php官网已很明确的进行了定义并形象化解释,这里直接从php官网copy一段文字[来源]. “什么是命名空间?从广义上来说,命名空间是一种封装事物的方法.在很多地方都可以见到这种抽象概念.例如,在操作系统中目录