+ -
当前位置:首页 → 问答吧 → 请问这里namespace的作用是什么?

请问这里namespace的作用是什么?

时间:2011-12-16

来源:互联网

在一个cpp文件中有这么一段代码
namespace
{
  bool fun1()
  {
  return 1;
  };

  bool fun2()
  {
  return 2;
  };
}

请问这里namespace的作用是什么? 谢谢!

作者: hjh2005   发布时间: 2011-12-16

相当于static bool func1(); static bool func2();

作者: qq120848369   发布时间: 2011-12-16

避免命名冲突的,相当于同一给每个标识的名字前面冠以前缀。

作者: mougaidong   发布时间: 2011-12-16

我想知道 如何定义在无名名字空间中声明的函数

作者: pwy198407   发布时间: 2011-12-16

引用 2 楼 mougaidong 的回复:

避免命名冲突的,相当于同一给每个标识的名字前面冠以前缀。


帅哥,没加前缀啊!

作者: hjh2005   发布时间: 2011-12-16

ISO C++ 7.3.1.1

The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D);the unnamed-namespace provides a superior alternative.

就是说,未命名名字空间取代static

作者: yisikaipu   发布时间: 2011-12-16

引用 5 楼 yisikaipu 的回复:

ISO C++ 7.3.1.1

The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D);the unnamed-namespace provides a superior alternative.

就是说,未命名名字空间取代static
……


3Q ,遗憾的是英语太差,这句话看不懂呀! 哈哈

作者: hjh2005   发布时间: 2011-12-16

引用 6 楼 hjh2005 的回复:
引用 5 楼 yisikaipu 的回复:

ISO C++ 7.3.1.1

The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D);the unnamed-namespace provides a superior alternative.……


就是匿名的名字空间,文件外部是不可以访问到的。因为没有名字。所以这样相当于是static 的效果。

作者: mingliang1212   发布时间: 2011-12-16