我知道如果要定义静态函数,C ++中首选命名空间,但是当你有访问其他函数的函数但其他函数应该是私有的而其他函数不可访问时该怎么做。如何声明/定义它?使用类不是很简单吗?或者我还必须使用命名空间吗?
像这样:
class Test1
{
public:
static void Start()
{
if(1) // Some checks...
{
ProcessStart();
}
}
private:
static void ProcessStart()
{
if(!Initialized)
{
//Initialize
}
// Do other stuff
}
static bool Initialized;
};
bool Test1::Initialized = false;
答案 0 :(得分:4)
您无需在公共API中公开私人详细信息:
<强> foo.hpp:强>
#ifndef H_FOO
#define H_FOO
namespace Foo
{
void Start();
}
#endif
<强> Foo.cpp中:强>
#include "foo.hpp"
namespace Foo
{
namespace
{
void ProcessStart()
{
// ...
}
}
void Start()
{
ProcessStart();
}
}
只有头文件最终出现在您的公共/include
目录中,因此没有人会知道有私有实现函数(除了您的调试器)。