简体   繁体   English

头文件和命名空间有什么区别?

[英]What is the difference between header file and namespace?

我想知道Header文件(如在MyHeader.hpp中)和c++namespace之间的确切区别?

Header files are actual files - stored in the file system, referenced by file name, and #include 'd in other files (at least, in C/C++ or other languages using the M4 macro preprocessor). 头文件是实际文件 - 存储在文件系统中,由文件名引用, #include在其他文件中(至少在C / C ++或使用M4宏预处理器的其他语言中)。 Header files typically group pieces of code that are all interdependent parts of the same specific item together. 头文件通常将代码片段组合在一起,这些代码片段是同一特定项目的所有相互依赖的部分。 For instance, a game might have a header file for all of its graphics rendering. 例如,游戏可能具有用于其所有图形渲染的头文件。

Namespaces, on the other hand, are an element of the programming language - they don't exist as a file system object, but rather as a designation within code telling the compiler that certain things are within that namespace. 另一方面,命名空间是编程语言的一个元素 - 它们不作为文件系统对象存在,而是作为代码中的一个指定,告诉编译器某些事物在该命名空间内。 Namespaces typically group interfaces (functions, classes/structs, types) of similar (but not necessarily interdependent) items. 命名空间通常将类似(但不一定是相互依赖)项的接口(函数,类/结构,类型)分组。 For instance, the std namespace in C++ contains all of the Standard Library functions and classes. 例如,C ++中的std命名空间包含所有标准库函数和类。

To know what is Header file, you need to know the meaning of "declaration". 要知道什么是Header文件,您需要知道“声明”的含义。

To put it in simple words, in C/C++, compilation happens in per-source manner. 换句话说,在C / C ++中,编译以每个源的方式进行。 If I have a A.cpp and inside I make use of a function foo(), which will be defined somewhere else, I need to tell the compiler that: "Hey, I am using foo(), although you cannot see it defined anywhere in my source, don't worry, it is defined in another source". 如果我有一个A.cpp并在里面我使用了一个函数foo(),它将被定义在其他地方,我需要告诉编译器:“嘿,我正在使用foo(),虽然你看不到它定义在我的源代码中的任何地方,不要担心,它在另一个来源中定义“。 They way to tell compiler about this is by "declaring" foo() in A.cpp. 他们告诉编译器这是通过在A.cpp中“声明”foo()。

If I am the author of foo(), everyone that use foo() need to write down the declaration void foo(); 如果我是foo()的作者,那么使用foo()的每个人都需要写下声明void foo(); in their source file. 在他们的源文件中。 It will be a lot of duplicated and meaningless work. 这将是许多重复和毫无意义的工作。 And it is so difficult for me to tell the guy that "use" foo() to have a correct declaration. 而且我很难告诉那个人“使用”foo()来获得正确的声明。 Therefore as the author of foo(), I write down a file, containing the declaration for using foo(), and distribute it so that people can just "import" the file content to their source. 因此,作为foo()的作者,我写下了一个文件,其中包含使用foo()的声明,并将其分发,以便人们可以将文件内容“导入”到它们的源。 The file I am distributing is Header file. 我分发的文件是Header文件。 The action of import is #include in C/C++. 导入的操作是C / C ++中的#include。 Yes, #include is nothing but inserting the content of the included file to the spot of #include. 是的,#include只是将包含文件的内容插入到#include的位置。


Namespace is another story. 命名空间是另一个故事。 To make it short, you can think of it is "real" name of function/class etc. for example, if I make 为了缩短它,你可以认为它是函数/类等的“真实”名称,例如,如果我做的话

namespace FOO {
  class Bar { }
}

The class is not really named Bar, it's "real" name is in fact FOO::Bar. 这个类并没有真正命名为Bar,它的“真实”名称实际上是FOO :: Bar。

C++ provides some way to save you typing the long real name, by "using". C ++提供了一些方法来保存您通过“使用”键入长实名。

A header file is a file that is intended to be included by source files. 头文件是供源文件包含的文件。 They typically contain declarations of certain classes and functions. 它们通常包含某些类和函数的声明。

A namespace enables code to categorize identifiers. 命名空间使代码能够对标识符进行分类。 That is, classes, functions, etc. can be placed inside a namespace, keeping those separate from other classes that are unrelated. 也就是说,类,函数等可以放在命名空间中,使它们与其他不相关的类分开。 For example, in C++ everything from the standard library is in the std namespace. 例如,在C ++中,标准库中的所有内容都在std命名空间中。

In common man langauge, Header file would be unique file on file system and namespace would be covering one or more files. 在普通人langauge中,头文件将是文件系统上的唯一文件,命名空间将覆盖一个或多个文件。

ie HeaderFile is physical thing and namespace is logical thing. 即HeaderFile是物理的东西,命名空间是逻辑的东西。

learn more about them here http://en.wikipedia.org/wiki/Namespace and http://en.wikipedia.org/wiki/Header_file http://en.wikipedia.org/wiki/Namespacehttp://en.wikipedia.org/wiki/Header_file了解更多相关信息

Namespace is a new concept of 'ansi c++' to identify global identifiers which are to be frequently used in your programme. 命名空间是'ansi c ++'的新概念,用于标识在程序中经常使用的全局标识符。

Header file is a source file that supports your program by reusing reliable and tested code hence saving time and effort!!!! 头文件是一个源文件,通过重用可靠和经过测试的代码来支持您的程序,从而节省时间和精力!!!!

Header file is basically the file system, it is physical thing, it contains the classes and functions if you didn't include header file and try to perform some operation like converting uppercase string to lower case, calculating string length you need to include header file string.h by which compile understand you are performing operation on string And while executing code it will link with corresponding library in which such functions reside On other hand namespace is a part of program, it is logical thing, it is designed for providing a way to keep one set of name separate from other. 头文件基本上是文件系统,它是物理的东西,它包含类和函数,如果你没有包含头文件,并尝试执行一些操作,如将大写字符串转换为小写,计算字符串长度,你需要包括头文件string.h通过它编译了解你正在对字符串执行操作而在执行代码时它会链接到相应的库,其中这些函数驻留在另一方面,命名空间是程序的一部分,它是逻辑的东西,它是为提供一种方式而设计的保持一组名称与其他名称分开。 The class name declared in one namespace does not conflict with the same class name declared in another . 在一个名称空间中声明的类名与在另一个名称空间中声明的相同类名不冲突。 All namespaces reside in .net framework class library which is huge collection of hierarchy of namespaces. 所有名称空间都驻留在.net框架类库中,这是名称空间层次结构的巨大集合。 Each namespaces contains classes, structures, interfaces, enumeration, delegates ...so on... 每个名称空间都包含类,结构,接口,枚举,委托......等等......

Namespace is new word given to headerfile. 命名空间是给headerfile的新单词。 To make new versions avaiable. 使新版本可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 header 和库文件有什么区别? - What is the difference between header and library file? 头文件和库之间有什么区别? - What's the difference between a header file and a library? “使用”名称空间和声明名称空间有什么区别? - What is the difference between “using” namespace and declaring namespace? .h(头文件)和.cpp文件有什么区别? - What is the difference between a .h(header file) and a .cpp file? 包含标头和C ++文件有什么区别? - What's the difference between including a header and a C++ file? c ++名称空间和使用之间有什么区别 - c++ What is the difference between namespace and using 编写“:: namespace :: identifier”和“namespace :: identifier”有什么区别? - What is the difference between writing “::namespace::identifier” and “namespace::identifier”? 在C++中使用标准库函数,它的头文件和std命名空间到底是什么关系? - For using a standard library function in C++, what exactly is the relationship between its header file and std namespace? 在头文件中定义的静态对象和在 cpp 文件中定义的静态对象有什么区别? - What is the difference between static objects defined in a header file and static objects defined in a cpp file? 内存标准头和包含文件之间的区别 - Difference between memory standard header and an include file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM