I have been making files like this for awhile: Does the order make sense? or should the namespace and the #includes be swapped and why.

#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
有帮助吗?

解决方案

Yes. That's looks good.

Though I order my headers differently (but alphabetically is fine).

The only thing I would change is the include guard. I make the include my namspace as well as the class name. As several times I have classes with the same name (but in a different namespace) being used by the same code.

#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

其他提示

What you've written is perfect. I don't think you need to change the order.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top