| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1504 人关注过本帖
标题:什么是命名空间?如何使用?
只看楼主 加入收藏
明天不一样
Rank: 1
等 级:新手上路
帖 子:121
专家分:0
注 册:2006-8-31
收藏
 问题点数:0 回复次数:1 
什么是命名空间?如何使用?
如题,什么是命名空间?如何使用,请举例说明
搜索更多相关主题的帖子: 空间 举例 
2006-11-14 19:01
lj_860603
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:714
专家分:0
注 册:2006-1-25
收藏
得分:0 

namespace(命名空间)

关键词namespace

A namespace declaration identifies and assigns a unique name to a user-declared namespace.
一个命名空间的声明标识符 和 赋值给一个用户使用命名空间的单一名字.
声明格式: namespace identifier
命名空间 标识符

定义实现的语法: namespace identifier
{
[ declaration-list ]
}

Such namespaces are used to solve the problem of name collision in large programs and libraries. Programmers can use namespaces to develop new software components and libraries without causing naming conflicts with existing components. [collision:难题 ;conflicts:冲突]
For example:

Copy Code
// namespace_declaration1.cpp
namespace X
{
int i;
double j;
}
int main()
{
X::i++;
}

命名空间的嵌套:
A namespace-definition can be nested within another namespace-definition. Every namespace-definition must appear either at file scope or immediately within another namespace-definition.

For example:
Copy Code
// namespace_declaration2.cpp
// C2870 expected
namespace A
{
int j = 3;
int f(int k);
}

namespace Outer
{
int n = 6;
int func(int num);

namespace Inner
{
float f = 9.993;
}
}

int main()
{
namespace local // C2870: not at global scope
{
}
}

Unlike other declarative regions, the definition of a namespace can be split over several parts of a single translation unit.
For example:
// namespace_declaration3.cpp
namespace A
{
// declare namespace A variables
int i;
int j;
}

namespace B
{
}

namespace A
{
// declare namespace A functions
void func(void);
int int_func(int i);
}

int main()
{
}


When a namespace is continued in this manner, after its initial definition, the continuation is called an extension namespace definition. The original definition of that namespace is known as an original namespace definition.

Usage of this notation might be cumbersome with longer names or in large programs. The using declaration, using directive, and namespace aliases provide more straightforward ways to reference namespace members.

A namespace declaration, whether it involves a new namespace, an unnamed namespace, or an extended namespace definition, must be accompanied by a namespace body enclosed within curly braces. The statement

Copy Code
namespace X;

is a syntax error. The statement

Copy Code
namespace X{};

is not a syntax error, but is meaningless.

For background information, see Namespaces.

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.

The declarative region of a namespace definition is its body. The body must be enclosed in curly braces ({}) and may contain declarations or definitions of variables, functions, objects, templates, and nested namespaces. The declarations in the declaration-list are said to be members of the namespace. The name of each namespace member is automatically qualified by the name of its namespace and the scope resolution operator.


我的原则很简单:不做不喜欢的事!
2006-11-14 20:41
快速回复:什么是命名空间?如何使用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025331 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved