我正在制作一个静态库,其中定义的所有东西都在一个名称空间中。但我不确定是应该像定义一个类一样定义方法,还是将它们包装在命名空间中。我要问的是:头文件声明和源代码定义的C++命名空间
这是有效的:
MyThings.h
namespace MyThings {
void DoStuff();
void DoOtherStuff();
}
MyThings.cpp
namespace MyThings {
void DoStuff() {
// Do this.
}
void DoOtherStuff() {
// Do that.
}
}
或者,我应该把它定义像我想的类方法?:
MyThings.cpp
void MyThings::DoStuff() {
// Do this.
}
void MyThings::DoOtherStuff() {
// Do that.
}
我不希望使用using namespace MyThings;
,我宁愿使用我的第一个例子,如果它是有效的,我觉得它使代码更具可读性,而不必在每个方法标识符前使用MyThings::
。
两者都有效。 –
这两种变体的效果都很好。这取决于你的想法。 –
相关(并提供第三个选项的讨论):http://stackoverflow.com/questions/10928686/best-practices-using-namespace-or-reopen-namespace –