C++之namespace
来源:互联网 发布:淘宝代付超限怎么办 编辑:程序博客网 时间:2024/05/16 04:52
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:
In this case, the variables a and b are normal variables declared within a namespace called myNamespace. 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:
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:
In this case, there are two global variables with the same name: var. One is defined within the namespace firstand the other one in second. No redefinition errors happen thanks to namespaces.
Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.
The keyword using can also be used as a directive to introduce an entire namespace:
In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace 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:
We can declare alternate names for existing namespaces according to the following format:
namespace new_name = current_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:
1234
namespace myNamespace{ int a, b;}
In this case, the variables a and b are normal variables declared within a namespace called myNamespace. 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:
12
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:
12345678910111213141516171819
// 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 namespace firstand 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:12345678910111213141516171819202122232425
// 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 whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.
The keyword using can also be used as a directive to introduce an entire namespace:
123456789101112131415161718192021222324
// 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 of x and y without name qualifiers were referring to their declarations in namespace 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:
12345678910111213141516171819202122232425
// 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 in iostream.
- c#namespace
- C++::namespace
- c-namespace
- C++namespace
- C++namespace
- C++-namespace(命名空间)
- C++-namespace(命名空间)
- C++namespace的使用
- C ++ Namespace (computer science)
- C++namespace命名空间
- 【Visual C++】关于namespace
- C/C++ namespace
- c++-tissue_habit-namespace
- C++namespace命名空间
- c++のnamespace
- [C++]namespace命名空间
- c++namespace于iostream
- C++:命名空间(namespace)
- Ubuntu 12.04开机“Ubuntu is running in low-graphics mode”解决方法
- usb hid 报告描述符详解
- installshield 2010 安装包
- Hello World! Build Your First iPhone App
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题
- C++之namespace
- Java反射经典实例
- SQL Server2000数据库附加提示“错误823“的快速修复
- java 通过 System 获取系统 属性列表
- Android开发listview控件使用
- NPAPI入门讲解
- Linux下编写动态链接库的简单过程
- java经典面试题(6)
- hdu 1046 数学公式推导