我目前的实施,简化:
#include <string>
#include <memory>
class Log
{
public:
~Log() {
// closing file-descriptors, etc...
}
static void LogMsg( const std::string& msg )
{
static std::unique_ptr<Log> g_singleton;
if ( !g_singleton.get() )
g_singleton.reset( new Log );
g_singleton->logMsg( msg );
}
private:
Log() { }
void logMsg( const std::string& msg ) {
// do work
}
};
Run Code Online (Sandbox Code Playgroud)
总的来说,我对此实现感到满意,因为:
但是,负面因素是:
所以这里是我的问题针对那些成功从他们的C++代码中驱逐所有单例的开发人员:
我想避免在我的代码中传递一个Log实例,如果可能的话 - 注意:我问,因为,如果有一个好的,合理的选择,我也想从我的代码中驱除所有Singletons.