如何定义名称空间以及编译器如何处理名称空间
how do define namespace and how does the compiler deal with namespace?
今天我看到C++命名空间,遇到了一个问题。编译器对命名空间做什么?例如:我们写
#include<iostream>
using namespace std;
那么问题来了,iostream
文件和namespace std
之间的关系是什么?std
在哪里定义,在什么文件中?当我使用#include <iostream.h>
时,我知道编译器会将iostream.h中的声明(如"cout"、"cin"等)带到我的cpp文件中。
你能给点建议吗?提前谢谢。
阅读本文,它解释了名称空间http://www.cplusplus.com/doc/tutorial/namespaces/
<iostream>
包含来自namespace
std
的项目。您可以将命名空间视为方法、类定义和变量的分组。使用名称空间使它们更容易按功能进行分组。
using
指令只是导入全局命名空间中命名空间的所有内容。但你不必使用它:
您可以使用:
using namespace std;
cout << "whatever";
或
std::cout << "whatever";
原因是编译器不知道名称空间之外的cout
是什么。
把它想象成:
//file <iostream>
namespace std
{
//declaration of cout
}
//file <vector>
namespace std
{
//declaration of vector
}
这种情况就像在库中搜索一样。iostream
是书,std
是页,cout
是行或段落。
注意:同一页面可以存在于多本书中
请在此处阅读有关命名空间的信息。
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 如何处理警告 C4177:#pragma 'float_control' 只能在全局范围或命名空间范围内使用
- 如何在 Linux 用户空间中处理类似 GPIO 中断的处理
- 如何处理boost :: fileSystem ::路径的空间
- 如何处理CPP中弦线中的空间
- 如何使我的功能通过大多数频繁以及如何处理空间来组织?C
- 分配额外的处理空间
- boost::iostreams::copy-sink-ENOSPC(设备上没有剩余空间)错误处理
- 如何处理正向声明中的命名空间
- 用户空间和内核空间进程中的信号处理程序集
- 如何处理不使用命名空间的库
- 如何处理堆栈外空间分段错误
- 如何定义名称空间以及编译器如何处理名称空间
- xerces-处理标记名称中的名称空间
- 为什么我们在Qt中处理表单时要为ui对象声明一个命名空间?
- 如何处理不断变化的c++ std::命名空间?例如:std::tr1::shared_ptr vs. std::shar
- 如何在c++中处理命名空间
- C++如何在可移植代码中处理tr1和非tr1命名空间