首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

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

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

[解决办法]
命名空间其实就是一个区域。

比如,你是184班的张三。

命名空间:184班;
变量名:张三。

如果人家喊999班的张三,那显然就不是在叫你。
[解决办法]
域:域内成员的有效范围。超出范围就是无效!
比如你在你家:你怎么捣腾你家都可以,都是合法的!但是你去捣腾别人家,你就违法了,
超出了你的作用范围了
[解决办法]
就是字面意思, 名字空间。
有点像姓氏, 作用就是避免重名。
[解决办法]
就为了避免重名弄的一种机制,类似于名字的前缀。
[解决办法]
和class类似, 封装隐藏.
[解决办法]
namespace abc {
  int x;
  int f() {
      ...
  }
}
跟类很类似,可以这样直接写实现。
也可以先在namespace里声明int f();
然后定义int abc::f()
{
}
以后在别的地方引用的时候可以直接
abc::x
也可以using namespace abc;
这样只用x就可以表明是abc::x了(前提是没有冲突)
[解决办法]
命名空间是为了避免有名字冲突.
比如说, 标准命名空间std 里面有标准函数, 类, 还有cout, cin 输入输出.
你也可以定义自己的命名空间, 然后里面也有cout, cin.
std::cout;
xxx::cout;
这样不冲突.
[解决办法]
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.

热点排行