47.命名空间namespace

版权声明:本博客为记录本人学习过程而开,内容大多从网上学习与整理所得,若侵权请告知! https://blog.csdn.net/Fly_as_tadpole/article/details/83448869

每个命名空间就是一个作用域。

命名空间是不连续,存在则追加,不存在则创建

内联命名空间,可以使得最外层的命名空间直接使用内层任何一层的成员名字,而无需添加中间层的命名空间的名字

未命名的命名空间中定义的成员具有静态生命周期,声明时创建,程序结束时销毁。内层未命名的命名空间可以直接使用外层命名空间的名字直接访问成员。

命名空间的别名

namespace abcdefj{

void hhhh() {std::cout<<"hello"<<std::endl;}};

namespace b = abcdefj;

b::hhhh();

命名空间与函数重载与二义性。

namespace a {
	namespace {
		void print(std::string &&s) { std::cout << "namespace::print" << std::endl; }
	}

	void print() { std::cout << "a::print" << std::endl; }

}

namespace b {

	void print(int &s) { std::cout << "b::print" << std::endl; }
}

namespace c{

	using namespace a;
	using namespace b;
	void print(double &&s) { std::cout << "c::print" << std::endl; }
}

int main()
{
	using namespace c;
	print("meiyusb");
	print(2.3);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Fly_as_tadpole/article/details/83448869