c++ namespace

一 :
和是不一样,前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。

后缀为.h的头文件c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。

因此,当使用时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。

二:
所谓namespace,是指标识符的各种可见范围。
C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。
由于namespace的概念,使用C++标准程序库的任何标识符时,可以有三种选择:

1、直接指定标识符。例如std::ostream而不是ostream。完整语句如下:
std::cout << std::hex << 3.4 << std::endl;

2、使用using关键字。
using std::cout;
using std::endl;

以上程序可以写成

cout << std::hex << 3.4 << endl;

3、最方便的就是使用using namespace std;

例如:

#include
#include
#include
using namespace std;
这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。那么以上语句可以如下写:

cout << hex << 3.4 << endl;

因为标准库非常的庞大,所程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以为了避免这种情况所造成的名字冲突,就把标准库中的一切都被放在名字空间std中。但这又会带来了一个新问题。无数原有的C++代码都依赖于使用了多年的伪标准库中的功能,他们都是在全局空间下的。  

         所以就有了和等等这样的头文件,一个是为了兼容以前的C++代码,一个是为了支持新的标准。

命名空间std封装的是标准程序库的名称,标准程序库为了和以前的头文件区别,一般不加".h"

===================

    namespace name {
        declaration-list;
    }

The namespace keyword allows you to create a new scope. The name is optional, and can be omitted to create an unnamed namespace. Once you create a namespace, you‘ll have to refer to it explicitly or use the using keyword. Example code:  

    namespace CartoonNameSpace {
           int HomersAge;
           void incrementHomersAge() {
             HomersAge++;
           }
       }
       int main() {
           ...
           CartoonNameSpace::HomersAge = 39;
           CartoonNameSpace::incrementHomersAge();
           cout << CartoonNameSpace::HomersAge << endl;
           ...
       }

anonymous namespace

    namespace {
        declaration-list;
        }

namespace alias

    namespace identifier = namespace-specifier;

可以分在不同的代码段中

    #include <iostream>
    #include <string>

    using namespace std;

    namespace zoo{
            class animal{
                    private:
                            int legs;
                    public:
                            animal(): legs(4){}

                            void say(void){
                                    cout << "legs:" << legs << endl;
                            }

            };

    }

    namespace zoo{
            class plant{
                    private:
                            string name;
                    public:
                            plant(): name("a_plant") {}
                            plant(string nm): name("a_plant") {
                                    name = nm;
                                    }
                            void say(void){
                                    cout << "name:" << name << endl;
                            }
            };
    }
    int main(void){

            zoo::animal dog;
            dog.say();

            /* error
             * ‘animal’ was not declared in this scope
             animal cat;
             cat.say();
             */

            zoo::plant flower("flower");
            flower.say();

    }

输出

    legs:4
    name:flower

参考:

http://www.kuqin.com/language/20080107/3532.html

http://www.cppreference.com
时间: 2024-05-24 19:56:46

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一段文字[来源]. “什么是命名空间?从广义上来说,命名空间是一种封装事物的方法.在很多地方都可以见到这种抽象概念.例如,在操作系统中目录