C++之 头文件
- In C++,separated .h and .cpp files are used to define one class.
- Class declaration and prototypes in that class are in the header file(.h).
- All the bodies of these functions are in the source file(.cpp).
- If a function is declared in a header file,you must include the header file everywhere the function is used and where the function is defined.
- If a class is declared in a header file,you must include the header file everywhere the class is used and where class member functions are defined.
- The header is a contract between you and the user of your code.
- The compile enforces the contract by requirig you to declare all structures and functions before they are used.
Declarations vs. Definitions
- A .cpp file is a compile unit
- Only declaratons are allowed to be in .h
- extern variables
- function prototypes //函数原型
- class/struct declaration
- #include is to insert the included file into the .cpp file at where the #include statement is.
- #include "xx.h":first search in the current directory,then the directories declared somewhere
- #include <xx.h>:search in the specified directories
- #include <xx>:same as #include <xx.h>
#ifndef HEADER_FLAG#define HEADER_FLAG//Type declaration here...#endif // HEADER_FLAG
- One class declaration per header file
- Associated with one source file in the same prefix of file name.
- The contents of a header file is surrounded with #ifndef #define #endif
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。