use*_*880 7 c++ implementation namespaces file
我在外部*.cpp文件中定义了一些函数(这里没有涉及的类),当然还有一个合适的*.h文件.
*.cpp文件中的某些函数仅用于其他地方的*.cpp文件中.*.h文件中甚至没有提到它们.
我应该将这些函数放入一个未命名的命名空间中,还是只能放在其他函数旁边?如果是这样,为什么我需要一个未命名的命名空间呢?我无法看到问题,因为无论如何都无法从外部访问这些功能.
Cha*_*had 18
如果您希望它们真正属于该编译单元,请将它们放在匿名命名空间中.如果你不这样做,那么有人可以在其他地方声明这些功能并明确地使用它们.
请看以下示例:
// library.cpp
// a "private" function here, in that it is not declared anywhere
void f() {}
namespace
{
// same as above, except within an anonymous namespace
void g() {}
}
// client.cpp
void f();
int main()
{
// Can call f(), it's been declared and is now effectively "public"
f();
// compilation error, this has no idea what g() is, it's not declared
// in any scope that can be resolved here
g();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5482 次 |
最近记录: |