【发布时间】:2012-10-05 10:17:05
【问题描述】:
我正在制作一个静态库,其中定义的所有内容都在一个命名空间中。但我不确定我是否应该像定义一个类一样定义方法,或者也将它们包装在命名空间中。我要问的是:
这是否有效:
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::。
【问题讨论】:
-
两种变体都同样有效。由你决定。
-
相关(并提供第三个选项的讨论):stackoverflow.com/questions/10928686/…
标签: c++ namespaces coding-style declaration