包括守护风格,C++

rec*_*gle 1 c++ coding-style include-guards

我有一个.h文件,其中包含几个类定义.我想在这个文件中使用C++的include guard; 但是,我想知道使用包含警卫的哪种方式被认为是正确/正确的?

一名警卫保护一切

#ifndef FOO_BAR
#define FOO_BAR

class Foo
{
};

class Bar
{      
};

#endif
Run Code Online (Sandbox Code Playgroud)

或多个单独的警卫.

#ifndef FOO
#define FOO

class Foo
{
};

#endif

#ifndef BAR
#define BAR

class Bar
{      
};

#endif
Run Code Online (Sandbox Code Playgroud)

Jos*_*ost 5

他们包括警卫,防止双重包含文件.所以它们应该按文件定义一次,而不是按类或函数定义.