在 #ifndef
和 #define
之后将 #endif
放在 c++ 头文件的开头是不好的做法吗?如果是,为什么?
This question没有触及为什么 #endif
在最后 - 这正是我在谷歌上搜索的内容。
例如
//cDate.h
#ifndef CDATE_H_EXISTS
#define CDATE_H_EXISTS
#include <string>
class cDate {
private:
std::string day;
std::string month;
std::string year;
public:
void setDate(std::string, std::string, std::string);
std::string getDate(int);
}; // end class def
#endif
对比
//cDate.h
#ifndef CDATE_H_EXISTS
#define CDATE_H_EXISTS
#endif
#include <string>
class cDate {
private:
std::string day;
std::string month;
std::string year;
public:
void setDate(std::string, std::string, std::string);
std::string getDate(int);
}; // end class def
最佳答案
第一个例子,是include guard
第二个例子,它是nothing-guard。
关于c++ - 为什么#endif 包含在.h 页面的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495627/