请教,网上看了一些资料,还是没明白命名空间是什么意思,请指教一下谢谢

summber_flower 2013-08-30 09:54:44
请教,网上看了一些资料,还是没明白命名空间是什么意思,请指教一下谢谢
...全文
7901 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
summber_flower 2013-10-11
  • 打赏
  • 举报
回复
多谢各位,结贴
赵4老师 2013-09-02
  • 打赏
  • 举报
回复
namespace Declaration C++ Specific —> namespace [identifier] { namespace-body } A namespace declaration identifies and assigns a name to a declarative region. The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members. The declarative region of a namespace declaration is its namespace-body. END C++ Specific Namespace Alias A namespace-alias is an alternative name for a namespace. Syntax namespace-alias : identifier namespace-alias-definition : namespace identifier = qualified-namespace-specifier; qualified-namespace-specifier : ::opt nested-name-specifieropt class-or-namespace-name A namespace-alias-definition declares an alternate name for a namespace. The identifier is a synonym for the qualified-namespace-specifier and becomes a namespace-alias. For example: namespace a_very_long_namespace_name { ... } namespace AVLNN = a_very_long_namespace_name; // AVLNN is now a namespace-alias for a_very_long_namespace_name. A namespace-name cannot be identical to any other entity in the same declarative region. In addition, a global namespace-name cannot be the same as any other global entity name in a given program. namespace Declaration A namespace declaration identifies and assigns a name to a declarative region. Syntax original-namespace-name : identifier namespace-definition : original-namespace-definition extension-namespace-definition unnamed-namespace-definition original-namespace-definition : namespace identifier { namespace-body } extension-namespace-definition : namespace original-namespace-name { namespace-body } unnamed-namespace-definition : namespace { namespace-body } namespace-body : declaration-seqopt The identifier in an original-namespace-definition must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members. Subsequently, in that declarative region, it is treated as an original-namespace-name. The declarative region of a namespace-definition is its namespace-body. A namespace can contain data and function declarations. The declaration-seq is a list of these declarations which are said to be members of the namespace. Defining namespace Members Members of a namespace may be defined within that namespace. For example: namespace X { void f() { } } Members of a named namespace can be defined outside the namespace in which they are declared by explicit qualification of the name being defined. However, the entity being defined must already be declared in the namespace. In addition, the definition must appear after the point of declaration in a namespace that encloses the declaration’s namespace. For example: namespace Q { namespace V { void f(); } void V::f() { } // ok void V::g() { } // error, g() is not yet a member of V namespace V { void g(); } } Unnamed namespaces An unnamed-namespace-definition behaves as if it were replaced by: namespace unique { namespace-body } using namespace unique; Each unnamed namespace has an identifier, represented by unique, that differs from all other identifiers in the entire program. For example: namespace { int i; } // unique::i void f() { i++; } // unique::i++ namespace A { namespace { int I; // A::unique::i int j; // A::unique::j } } using namespace A; void h() { I++; //error: unique::i or A::unique::i A::i++; // error: A::i undefined j++; // A::unique::j++ } 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.
summber_flower 2013-09-02
  • 打赏
  • 举报
回复
好吧,我把命名空间理解成是一个文件夹吧? 那么不在命名空间里定义的变量是啥呢? 如果命名空间是文件夹,那么这个namespace里的变量是全局的呢?还是只在这个namespace里有效,可见?
引用 12 楼 mmc1206x 的回复:
命名空间是为了避免有名字冲突. 比如说, 标准命名空间std 里面有标准函数, 类, 还有cout, cin 输入输出. 你也可以定义自己的命名空间, 然后里面也有cout, cin. std::cout; xxx::cout; 这样不冲突.
落单的毛毛虫 2013-09-02
  • 打赏
  • 举报
回复
命名空间是为了避免有名字冲突. 比如说, 标准命名空间std 里面有标准函数, 类, 还有cout, cin 输入输出. 你也可以定义自己的命名空间, 然后里面也有cout, cin. std::cout; xxx::cout; 这样不冲突.
5t4rk 2013-09-01
  • 打赏
  • 举报
回复
就是你在某个文件区域下,自己定义实现的一些函数,类,结构之类的。
todd_leftcode 2013-08-31
  • 打赏
  • 举报
回复
就是字面意思, 名字空间。 有点像姓氏, 作用就是避免重名。
summber_flower 2013-08-31
  • 打赏
  • 举报
回复
多谢,还是不是很理解 C++ PRIMER 讲的很难理解 那我的自定义函数和变量怎么放到我自己的命名空间呢?
小耸 2013-08-31
  • 打赏
  • 举报
回复
在命名空间A中声明的全局变量a,只在命名空间a中有效。
qq120848369 2013-08-31
  • 打赏
  • 举报
回复
和class类似, 封装隐藏.
zhcosin 2013-08-31
  • 打赏
  • 举报
回复
就为了避免重名弄的一种机制,类似于名字的前缀。
碼上道 2013-08-31
  • 打赏
  • 举报
回复
命名感觉像是在圈地
summber_flower 2013-08-31
  • 打赏
  • 举报
回复
谢谢 我把命名空间理解成是一个owner可以吗? 即谁拥有某个变量? 那么我不在命名空间里定义的变量算什么呢> 全局变量?
引用 8 楼 Bokutake 的回复:
namespace abc { int x; int f() { ... } } 跟类很类似,可以这样直接写实现。 也可以先在namespace里声明int f(); 然后定义int abc::f() { } 以后在别的地方引用的时候可以直接 abc::x 也可以using namespace abc; 这样只用x就可以表明是abc::x了(前提是没有冲突)
辰岡墨竹 2013-08-31
  • 打赏
  • 举报
回复
namespace abc { int x; int f() { ... } } 跟类很类似,可以这样直接写实现。 也可以先在namespace里声明int f(); 然后定义int abc::f() { } 以后在别的地方引用的时候可以直接 abc::x 也可以using namespace abc; 这样只用x就可以表明是abc::x了(前提是没有冲突)
max_min_ 2013-08-30
  • 打赏
  • 举报
回复
域:域内成员的有效范围。超出范围就是无效! 比如你在你家:你怎么捣腾你家都可以,都是合法的!但是你去捣腾别人家,你就违法了, 超出了你的作用范围了
super_admi 2013-08-30
  • 打赏
  • 举报
回复
命名空间其实就是一个区域。 比如,你是184班的张三。 命名空间:184班; 变量名:张三。 如果人家喊999班的张三,那显然就不是在叫你。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧