C++的namespace

使用using namespace xxx;你所使用的库函数或变量就是在该名字空间xxx中定义的.这样一来就不会引起不必要的冲突了.
/**********************************************************************
* Compiler: gcc 4.5.1 20100924 (Red Hat 4.5.1-4)
* Last Update: Mon 28 May 2012 06:49:24 PM CST
* File Name: one.h
* Description:
************************************************************************/
#include <iostream>
class test1{
public:
    void f(void){std::cout<<"One"<<std::endl;}
};
/**********************************************************************
* Compiler: gcc 4.5.1 20100924 (Red Hat 4.5.1-4)
* Last Update: Mon 28 May 2012 06:56:59 PM CST
* File Name: two.h
* Description:
************************************************************************/
#include <iostream>
class test1{
public:
    void f(void){std::cout<<"Two"<<std::endl;}
};
/**********************************************************************
* Compiler: gcc 4.5.1 20100924 (Red Hat 4.5.1-4)
* Last Update: Sun 27 May 2012 03:11:50 PM CST
* File Name: 1.cpp
* Description:
************************************************************************/
#include <iostream>
#include "one.h"
#include "two.h"
int main(int argc, char **argv)
{
    test1 t;
    t.f();

    return 0;
}
如果按照上述方式定义,那么这两个头文件不可能包含在同一个程序中,因为test1类会发生冲突。
进行编译会出现
[test1@localhost tempCode]$ g++ -o 1 1.cpp
In file included from 1.cpp:9:0:
two.h:8:7: error: redefinition of ‘class test1’
one.h:8:12: error: previous definition of ‘class test1’

namespace允许类,对象,函数聚集在一个名字下。
用法:
namespace 空间名{......}
/**********************************************************************
* File Name: one.h
************************************************************************/
#include <iostream>
namespace one
{
    class test1{
    public:
        void f(void){std::cout<<"One"<<std::endl;}
    };
}

/**********************************************************************
* File Name: two.h
************************************************************************/
#include <iostream>
namespace two
{
    class test1{
    public:
        void f(void){std::cout<<"Two"<<std::endl;}
    };
}

命名空间的使用方法有3种。
1、直接指定标识符,如one::test1,  two::test2
/**********************************************************************
* File Name: 1.cpp
************************************************************************/
#include <iostream>
#include "one.h"
#include "two.h"
int main(int argc, char **argv)
{
    one::test1 t1;
    two::test1 t2;
    t1.f();
    t2.f();

    return 0;
}
[test1@localhost tempCode]$ g++ -o 1 1.cpp
[test1@localhost tempCode]$ ./1
One
Two
2、使用using关键字。 using one::test1;
/**********************************************************************
* Compiler: gcc 4.5.1 20100924 (Red Hat 4.5.1-4)
* Last Update: Mon 28 May 2012 07:14:57 PM CST
* File Name: 2.cpp
* Description:
************************************************************************/
#include <iostream>
#include "one.h"
#include "two.h"
using one::test1;
int main(int argc, char **argv)
{
    test1 t1;
    two::test1 t2;
    t1.f();
    t2.f();

    return 0;
}
[test1@localhost tempCode]$ g++ -o 1 2.cpp
[test1@localhost tempCode]$ ./1
One
Two
3、最方便的就是使用using namespace one;这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。
/**********************************************************************
* Compiler: gcc 4.5.1 20100924 (Red Hat 4.5.1-4)
* Last Update: Mon 28 May 2012 07:14:57 PM CST
* File Name: 2.cpp
* Description:
************************************************************************/
#include <iostream>
#include "one.h"
#include "two.h"
using namespace one;
int main(int argc, char **argv)
{
    test1 t1;
    two::test1 t2;
    t1.f();
    t2.f();

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值