头文件,命名空间

小代帅帅 2013-09-09 08:03:02
我们知道C中没有命名空间,我们要加上头文件.h C++有自己的命名空间加上using namespace std;就能使用了(或是用iostream.h),
C中的头文件与C++中的命名空间是不是作用一样,里边包含的内容也一样呢?


C++ p11 中部
...全文
308 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2013-09-22
  • 打赏
  • 举报
回复
名空间就像一个有名字的篱笆,里面的都是名空间内部的,而且可以在任何文件里,不止一处的圈地。 同一文件,出现两处以上的,同一名空间定义,各个定义一份,无命名冲突的代码,不是一种错误。 名空间,可以通过using 指令,开放部分或者全部名字。 using namespace std;//开放整个名空间。cin,cout等等std中定义的名字,都可以直接使用。 using std::cin;//开放std::cin ,可以用cin 代替std::cin
赵4老师 2013-09-12
  • 打赏
  • 举报
回复
#include The #include directive causes resource compiler to process the file specified in the filename parameter. This file should be a header file that defines the constants used in the resource-definition file. The file can use single-byte, double-byte, or Unicode characters. Use the statement #ifndef RC_INVOKED in your header file to surround statements that can be compiled by a C compiler but not RC. This way, you can use the same include files in your .C and .RC files. Syntax #include filename Parameters filename Specifies the name of the file to be included. If the file is in the current directory, the string must be enclosed in double quotation marks; if the file is in the directory specified by the INCLUDE environment variable, the string must be enclosed in less-than and greater-than characters (<>). You must give a full path enclosed in double quotation marks (") if the file is not in the current directory or in the directory specified by the INCLUDE environment variable. Example This example processes the header files Windows.H and MyDefs.H while compiling the resource-definition file: #include <windows.h> #include "headers\mydefs.h" See Also #define -------------------- 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.
kelvin_wangning 2013-09-12
  • 打赏
  • 举报
回复
你可以认为C和C++是两种不同的语言,这样,你如果混淆,你就学不下去了 一个老师就这么说,你就看这这样就是对的,不要想为什么,你以后会知道为什么 现在跟你说,你也不懂,为什么要这样写 反正初学者,这样写,你知道是这样就对了, 规范写法也很重要
ForestDB 2013-09-10
  • 打赏
  • 举报
回复
两码事。。。
__GUNS_N_ROSES__ 2013-09-10
  • 打赏
  • 举报
回复
头文件和命名空间没有半毛钱关系。 包含头文件就是简单的把其他文件的内容加入到执行#include的那个文件里和它下边的代码一起参与编译罢了。而且不一定得是.h,随便你什么后缀,要不要后缀都可以,你高兴的话包含个cpp也没问题。 对于iostream,之所以用所谓C++方式#include<iostream>和C方式#include<iostream.h>之间,后者有不需要using namespace std;的区别,是因为编译器本身提供了两份头文件,.h的版本大概是内部已经帮你using namespace了。 像一些比较新的编译器,比如vs2012里带的,写iostream.h会报找不到文件的,想必是没提供.h的版本吧。毕竟这种过渡性的东西也没有长期留着的必要。
cjfdqchwhj 2013-09-10
  • 打赏
  • 举报
回复
命名空间 和 头文件 是两码事
zhyajshhz 2013-09-10
  • 打赏
  • 举报
回复
C和C++都有头文件的概念,都需要引入当前文件需要的文件参与编译。命名空间是为了防止命名冲突而引入的一种机制,看一下大概的机制。 在a.h文件中: int f() { return 1; } 在b.h文件中: namespace m{ int f(){return 1;} } 要使用a.h文件中f方法只需要#include "a.h", 但是要使用b.h文件中的f方法时首先需要#include "b.h",然后还得using namespace m;
七擒关羽 2013-09-10
  • 打赏
  • 举报
回复
2者不一样,不同的概念
图灵狗 2013-09-10
  • 打赏
  • 举报
回复
头文件都是一样的,区别只是在于C没有命名空间。
引用 楼主 u012012731 的回复:
我们知道C中没有命名空间,我们要加上头文件.h C++有自己的命名空间加上using namespace std;就能使用了(或是用iostream.h), C中的头文件与C++中的命名空间是不是作用一样,里边包含的内容也一样呢? C++ p11 中部
clarence_cpp 2013-09-09
  • 打赏
  • 举报
回复
可以这么说,c++也是包含头文件的

33,317

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧