namespace

    在大型项目中,可能会调用很多库文件,因此很有可能产生命名冲突的现象,为了解决这一问题,c++引入了namespace,开发人员可以使用此技术来避免命名冲突。

    (1)你可以对一个独立的函数添加命名空间,也可以对一个class添加命名空间,例如:

//tNameSpace.cpp
//对一个独立函数添加namespace
namespace printSpace
{
	void printFunction()
	{
		std::cout<<"hello word!!"<<std::endl;
	}
}

//对一个类添加namespace.
	namespace test_2
	{
		class t_nameSpace2
		{
		public:
			t_nameSpace2();
			~t_nameSpace2();
			std::string getName();
		private:
			std::string m_name;
		};
	}
对一个类添加命名空间时,在其实现时也需要对其添加命名空间,例如:

	namespace t_nameSpace2
	{
		t_nameSpace2::t_nameSpace2():m_name("tNameSpace2")
		{
		
		}

		t_nameSpace2::~t_nameSpace2()
		{

		}

		std::string t_nameSpace2::getName()
		{
			return m_name;
		}
		
	}
或者采用这种方式:

	test_2::t_nameSpace2::t_nameSpace2():m_name("tNameSpace2")
	{
		
	}

	test_2::t_nameSpace2::~t_nameSpace2()
	{

	}

	std::string test_2::t_nameSpace2::getName()
	{
		return m_name;
	}

   

(2) 命名空间支持嵌套,你可以在一个命名空间内,嵌套一个或者多个命名空间,例如:

	namespace t_test2
	{
		//...
		namespace t_NameSpace
			{
				//....

				namespace t_NameSpace_1
					{
						//...
						void printFun()
						{
							//...
						}						
					}
			}
	}
      

  (3)using 指令
        在调用命名空间内的元素(函数、类、变量)时,需要在元素前加命名空间,表明此元素属于该命名空间,例如:
    test_2::t_nameSpace2.
    但是可以通过使用using namespace 指令来省去在元素前面指定命名空间,编译器在编译阶段会自动确定该元素属于哪个命名空间。
    例如:

	// testPro1.cpp : 定义控制台应用程序的入口点。
	//
	#include "tNameSpace.h"
	#include <iostream>
	namespace t_space1
	{
		void fun1()
		{
			std::cout<<"in the  space \\t_space1\\"<<std::endl;
		}
	}
	namespace t_space2
	{
		void fun2()
		{
			std::cout<<"in the space \\t_space2\\"<<std::endl;
		}
	}
	using namespace t_space1;
	int main(int argc, char* argv[])
	{
		fun1();
		t_space2::fun2();
		system("pause");
		return 0;
	}

    输出结果:
    in the  space \t_space1\
    in the space \t_space2\


    (4) 命名空间别名
    大型项目中,可能会出现命名空间的多重嵌套,例如:
    要调用t_nameSpace内的元素,不使用using指令的情况下,调用printFun(),需要这样写:
    t_test2::t_nameSpace1::t_nameSpace2::printFun()
    这时,可以使用命名空间别名来简化因嵌套带来的冗余,例如:
    namespace t_ns2 = t_test2::t_nameSpace1::t_nameSpace2;
    简化后,可以这样调用printFun():
    t_ns2::printFun();
    当然也可以通过using指令到达简化的目的,eg:
    using t_test2::t_nameSpace1::t_nameSpace2;
    这时,直接调用printFun即可。


    (5) 无名称的命名空间(unnamed namespaces)
    命名空间定义的格式为
    namespace [identifer] { //....}
    省略identifer就是unnamed namespace,例如:

// testPro1.cpp : 定义控制台应用程序的入口点。
//
#include "tNameSpace.h"
#include <string>
#include <iostream>
namespace
{
	std::string g_str("in the unnamed namespace"); 
}

int main(int argc, char* argv[])
{
	std::cout<<g_str<<std::endl;
	system("pause");
	return 0;
}
输出结果:

    in the unnamed namespace.


    5.1 unamed namespaces和static function
    Unnamed namespaces are a superior replacement for the static declaration of variables. They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.

    The C++ Standard reads in section 7.3.1.1 Unnamed namespaces, paragraph 2:
    $7.3.1.1/2 - "The use of the static keyword is deprecated when declaring variables in a namespace scope (see annex D); the unnamed-namespace provides a superior alternative."                

    
    大体的意思就是推荐使用unnamed namespace不推荐使用static function。




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值