为什么#endif包含在.h页面的末尾?

Ula*_*ach 0 c++

#endif#ifndef和之后放在c ++头文件的开头是不好的做法#define?如果是这样,为什么?

这个问题没有涉及为什么#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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Tre*_*vor 7

第一个例子,它包括后卫

第二个例子,它是无关紧要的.