18

我一直在制作这样的文件:这个顺序有意义吗?或者应该交换命名空间和#includes 以及为什么。

#ifndef CLASSNAME_H // header guards
#define CLASSNAME_H

#include "a.h" // includes in alphabetical order
#include "b.h" // user specified includes first
#include "c.h"
#include <vector> // then library includes

namespace MyNamespace
{
    class ClassName
    {

    };
}

#endif
4

2 回答 2

12

是的。这看起来不错。

虽然我以不同的方式排列我的标题(但按字母顺序很好)。

我唯一要改变的是包含守卫。我将包含我的名称空间以及类名。有几次我有相同的代码使用同名的类(但在不同的命名空间中)。

#ifndef MY_COMPANY_MY_NAME_SPACE_MYCLASSNAME_H // header guards
#define MY_COMPANY_MY_NAME_SPACE_MYCLASSNAME_H

#include "a.h" //   includes in order of most specific to most general.

               //   My includes first.
               //   Then C++ headers          <vector>
               //        I group all the containers together.
               //   Then C specific headers   <sys/bla.h>
               //   Then C generic headers    <ctype.h>


namespace MyNamespace
{
    Class ClassName
    {

    };
}

#endif
于 2011-10-03T17:01:08.327 回答
4

你写的很完美。我认为您不需要更改顺序。

于 2011-10-03T16:58:03.343 回答