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

标签 c++

#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/

相关文章:

C++ 部分概念 id : What is the reason for the explicit template specification order/special status of first argument?

c++ - c++/c++11 中的 bool()、int() 和 double() 是什么?

c++ - 安卓。是否可以在 C/C++ 上编写自定义函数并在 SQL 查询中使用它?

c++ - 对 C++ 中的基本递归感到困惑

c++ - boost::bind 不能与条件表达式一起使用?

c++ - 另一个类的类指针数组的类指针数组的段错误

c++ - 在命名空间的类中使用带有枚举的 boost::program_options

c++ - 在 VSP 中为 apache 编译 passenger 时出现错误 "out of memory allocating"

c++ - 使用 malloc 在程序集中分配的内存 - 想将其转换为 C++ 中的三维数组

c++ - Eigen3 JacobiSVD 不同的奇异值取决于编译器标志