將#endif
放在#ifndef
和#define
之後的C++頭文件的開頭是否是不好的做法?如果是這樣,爲什麼?爲什麼在.h頁面的結尾處包含#endif?
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
VS
//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
問自己「#ifndef CDATE_H_EXISTS」的用途是什麼...... –
在C++頭文件的開始處放置#endif是否違反了#ifdef預處理指令的全部目的:爲了避免頭文件的內容被相同的源文件多次包含。 –
[爲什麼在C++頭文件中使用#ifndef和#define](http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files ) –