c++規範之 命名空間

來源:互聯網
上載者:User
文章目錄
  • using
  • Namespace alias
  • Namespace std

Namespaces allow to group entities like classes, objects and functions under a name.This way the global scope can be divided in "sub-scopes", each one with its own name.

The format of namespaces is:

namespace identifier
{
entities
}

Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example:

1
2
3
4
namespace myNamespace{  int a, b;}

In this case, the variables a and b are normal variables declared within a namespace calledmyNamespace. In order to access these variables from outside the
myNamespace namespace we have to use the scope operator::. For example, to access the previous variables from outside
myNamespace we can write:

1
2
myNamespace::amyNamespace::b 

The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// namespaces#include <iostream>using namespace std;namespace first{  int var = 5;}namespace second{  double var = 3.1416;}int main () {  cout << first::var << endl;  cout << second::var << endl;  return 0;}
53.1416

In this case, there are two global variables with the same name: var. One is defined within the namespacefirst and the other one in
second. No redefinition errors happen thanks to namespaces.

using

The keyword using is used to introduce a name from a namespace into the current declarative region. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// using#include <iostream>using namespace std;namespace first{  int x = 5;  int y = 10;}namespace second{  double x = 3.1416;  double y = 2.7183;}int main () {  using first::x;  using second::y;  cout << x << endl;  cout << y << endl;  cout << first::y << endl;  cout << second::x << endl;  return 0;}
52.7183103.1416

Notice how in this code, x (without any name qualifier) refers to first::x whereasy refers to
second::y, exactly as our using declarations have specified. We still have access tofirst::y and
second::x using their fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// using#include <iostream>using namespace std;namespace first{  int x = 5;  int y = 10;}namespace second{  double x = 3.1416;  double y = 2.7183;}int main () {  using namespace first;  cout << x << endl;  cout << y << endl;  cout << second::x << endl;  cout << second::y << endl;  return 0;}
5103.14162.7183

In this case, since we have declared that we were using namespace first, all direct uses ofx and
y without name qualifiers were referring to their declarations innamespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and
then those of another one, we could do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// using namespace example#include <iostream>using namespace std;namespace first{  int x = 5;}namespace second{  double x = 3.1416;}int main () {  {    using namespace first;    cout << x << endl;  }  {    using namespace second;    cout << x << endl;  }  return 0;}
53.1416
Namespace alias

We can declare alternate names for existing namespaces according to the following format:

namespace new_name = current_name;

Namespace std

All the files in the C++ standard library declare all of its entities within the
std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined iniostream.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.