什么应该进入.h文件? [英] What should go into an .h file?

查看:134
本文介绍了什么应该进入.h文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你的代码分成多个文件,什么应该进入一个.h文件,什么应该进入.cpp文件?

When dividing your code up into multiple files just what exactly should go into an .h file and what should go into a .cpp file?

推荐答案

标题文件( .h )旨在提供信息这将需要在多个文件。例如类声明,函数原型和枚举通常放在头文件中。一句话,定义。

Header files (.h) are designed to provide the information that will be needed in multiple files. Things like class declarations, function prototypes, and enumerations typically go in header files. In a word, "definitions".

代码文件( .cpp )旨在提供实现信息只需要在一个文件中知道。一般来说,函数体和应该/将永远不会被其他模块访问的内部变量是属于 .cpp 文件中的内容。一句话,实现。

Code files (.cpp) are designed to provide the implementation information that only needs to be known in one file. In general, function bodies, and internal variables that should/will never be accessed by other modules, are what belong in .cpp files. In a word, "implementations".

要问自己确定什么属于哪里是最简单的问题是如果我改变这个,我必须改变其他文件使事情再次编译?如果答案是是,它可能属于头文件;如果答案是否,它可能属于代码文件。

The simplest question to ask yourself to determine what belongs where is "if I change this, will I have to change code in other files to make things compile again?" If the answer is "yes" it probably belongs in the header file; if the answer is "no" it probably belongs in the code file.

这篇关于什么应该进入.h文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆