天天看点

c#创建命名空间_在C ++中创建和使用命名空间

c#创建命名空间

Namespace is a container for identifiers. It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace.

命名空间是标识符的容器。 它将其成员的名称放在一个不同的空间中,以便它们与其他名称空间或全局名称空间中的名称不冲突。

创建一个命名空间 (Creating a Namespace)

Creating a namespace is similar to creation of a class.

创建名称空间类似于创建类。

namespace MySpace
{
    // declarations
}

int main() 
{
    // main function
}
           

This will create a new namespace called

MySpace

, inside which we can put our member declarations.

这将创建一个名为

MySpace

的新命名空间,我们可以在其中放置成员声明。

创建命名空间的规则 (Rules to create Namespaces)

  1. The namespace definition must be done at global scope

    , or nested inside another namespace.

    名称空间定义必须在

    全局范围内 完成,或嵌套在另一个名称空间内。
  2. Namespace definition doesn't terminates with a semicolon like in class definition.

    命名空间定义不以类定义中的分号结尾。

  3. You can use an alias name for your namespace name, for ease of use.

    您可以为名称空间名称使用别名,以方便使用。

    Example for Alias: 别名示例:
    namespace StudyTonightDotCom
    {
        void study();
        class Learn 
        {  
            // class defintion
        };
    }
    
    // St is now alias for StudyTonightDotCom
    namespace St = StudyTonightDotCom;
               
  4. You cannot create instance of namespace.

    您无法创建名称空间的实例。

  5. There can be unnamed

    namespaces too. Unnamed namespace is unique for each translation unit. They act exactly like named namespaces.

    也可以有

    命名的名称空间。 未命名的名称空间对于每个翻译单元都是唯一的。 它们的行为与命名空间完全相同。 Example for Unnamed namespace: 未命名命名空间的示例:
    namespace
    {
        class Head 
        { 
            // class defintion
        };
        // another class
        class Tail 
        { 
            // class defintion
        };
        int i,j,k;
    }
    
    int main() 
    { 
        // main function
    }
               
  6. A namespace definition can be continued and extended over multiple files, they are not redefined or overriden.

    名称空间定义可以继续并扩展到多个文件,而不会被重新定义或覆盖。

    For example, below is some

    header1.h

    header file, where we define a namespace:

    例如,下面是一些

    header1.h 头文件,其中定义了名称空间:
    namespace MySpace
    {
        int x;
        void f();
    }
               
    We can then include the header1.h header file in some other header2.h

    header file and add more to an existing namespace:

    然后,我们可以将

    header1.h 头文件包含在其他一些 header2.h 头文件中,并向现有名称空间添加更多内容:
    #include "header1.h";
    
    namespace MySpace
    {
        int y;
        void g();
    }
               

在C ++中使用命名空间 (Using a Namespace in C++)

There are three ways to use a namespace in program,

在程序中使用命名空间的方法有以下三种:

  1. Scope resolution operator (

    ::

    )

    范围解析运算符(

    ::

  2. The

    using

    directive

    using

    指令
  3. The

    using

    declaration

    using

    声明

使用范围解析运算符(

::

(With Scope resolution operator (

::

))

Any name (identifier) declared in a namespace can be explicitly specified using the namespace's name and the scope resolution

::

operator with the identifier.

可以使用名称空间的名称和带标识符的范围解析

::

运算符来明确指定在名称空间中声明的任何名称(标识符)。

namespace MySpace
{
    class A
    {
        static int i;
        public:
        void f();
    };
    
    // class name declaration
    class B;    
    
    //gobal function declaration
    void func();   
}

// Initializing static class variable
int MySpace::A::i=9;      

class MySpace::B
{
    int x;
    public:
    int getdata()
    {
        cout << x;
    }
    // Constructor declaration
    B();   
}

// Constructor definition
MySpace::B::B()   
{
    x=0;
}
           

using

指令 (The

using

directive)

using

keyword allows you to import an entire namespace into your program with a global scope. It can be used to import a namespace into another namespace or any program.

using

关键字允许您

using

全局范围将整个命名空间导入程序。 它可用于将一个名称空间导入另一个名称空间或任何程序。

Conside a header file

Namespace1.h

:

考虑头文件

Namespace1.h

namespace X
{
    int x;
    class Check
    {
        int i;
    };
}
           

Including the above namespace header file in

Namespace2.h

file:

Namespace2.h

文件中包含上述名称空间头文件:

include "Namespace1.h";

namespace Y
{
    using namespace X;
    Check obj;
    int y;
}
           

We imported the namespace

X

into namespace

Y

, hence class

Check

will now be available in the namespace

Y

.

我们将命名空间

X

导入到命名空间

Y

,因此类

Check

现在将在命名空间

Y

可用。

Hence we can write the following program in a separate file, let's say

program1.cpp

因此,我们可以将以下程序编写在一个单独的文件中,例如

program1.cpp
#include "Namespace2.h";

void test()
{
    using Namespace Y;
    // creating object of class Check
    Check obj2;
}
           

Hence, the

using

directive makes it a lot easier to use namespace, wherever you want.

因此,

using

指令使在任何地方使用命名空间变得更加容易。

using

声明 (The

using

declaration)

When we use

using

directive, we import all the names in the namespace and they are available throughout the program, that is they have a global scope.

当使用

using

指令时,我们将所有名称导入命名空间中,并且它们在整个程序中都可用,即它们具有全局作用域。

But with

using

declaration, we import one specific name at a time which is available only inside the current scope.

但是通过

using

声明,我们一次导入一个特定名称,该名称仅在当前作用域内可用。

NOTE

: The name imported with

using

declaration can override the name imported with

using

directive

注意

using

声明导入的名称可以覆盖

using

指令导入的名称

Consider a file

Namespace.h

:

考虑一个文件

Namespace.h

namespace X
{
    void f()
    {
        cout << "f of X namespace\n";
    }
    void g() 
    {
        cout << "g of X namespace\n";
    }
}

namespace Y
{
    void f()
    {
        cout << "f of Y namespace\n";
    }
    void g() 
    {
        cout << "g of Y namespace\n";
    }
}
           

Now let's create a new program file with name

program2.cpp

with below code:

现在,使用以下代码创建一个名为

program2.cpp

的新程序文件:

#include "Namespace.h";

void h()
{
    using namespace X;  // using directive
    using Y::f; // using declaration
    f();    // calls f() of Y namespace
    X::f(); // class f() of X namespace
}
           

f of Y namespace f of X namespace

Y名称空间的f X命名空间的f

In using declaration, we never mention the argument list of a function while importing it, hence if a namespace has overloaded function, it will lead to ambiguity.

在使用声明时,我们在导入函数时从不提及函数的参数列表,因此,如果命名空间具有重载的函数,则会导致歧义。

翻译自: https://www.studytonight.com/cpp/namespace-in-cpp.php

c#创建命名空间