myMath.h
#include <stdio.h>
#include <math.h>
add... something
add... something
or
myMath.cpp
#include <stdio.h>
#include <math.h>
add... something
add... something
包含头文件的最佳方式是什么?
我知道 .h 包含最少的内容会更好,因为 #include 只是复制和过去
最佳答案
指导原则是每个 .h 文件必须是自给自足的——它应该无需任何更多代码行即可编译。
测试 myMath.h 是否自给自足的最佳方法是使其成为 myMath.cpp 中#include
的第一个文件。如果有任何编译器错误,则意味着 myMath.h 不能自给自足。
myMath.cpp:
#include "myMath.h"
// Rest of the file.
另一个指导原则是 .h 文件不得#include
任何其他头文件,除非它需要其中的某些内容。您可以从头文件中删除 #include
行,只要删除它们不会违反自给自足准则即可。
关于c++ - 在 .cpp 或 .h 中包含头文件的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40123185/