What is the difference between header file and namespace?

前端 未结 6 912
臣服心动
臣服心动 2020-12-29 10:09

I want to know the exact difference between Header file (as in MyHeader.hpp) and a namespace in c++?

6条回答
  •  盖世英雄少女心
    2020-12-29 10:13

    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). 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.

提交回复
热议问题