【发布时间】:2012-09-08 00:32:14
【问题描述】:
我有一个 .h 文件,其中包含几个类定义。我想在这个文件中使用 C++ 的包含防护;但是,我想知道哪种使用包含防护的方式被认为是正确/正确的?
一个守卫保护一切
#ifndef FOO_BAR
#define FOO_BAR
class Foo
{
};
class Bar
{
};
#endif
或多个单独的守卫。
#ifndef FOO
#define FOO
class Foo
{
};
#endif
#ifndef BAR
#define BAR
class Bar
{
};
#endif
【问题讨论】:
-
一个就够了。两个类都应该只定义一次,这发生在守卫内部的任何东西上。
标签: c++ coding-style include-guards