-, , . .
, ( -, , ), Singleton. , .
, - boost:: once.
- , Foo - .
3 Foo private
Foo* Foo::instance = NULL;
boost::once_flag Foo::flag = BOOST_ONCE_INIT;
void Foo::init()
{
Foo::instance = new Foo;
};
Foo & Foo::getInstance()
{
boost::call_once(init, Foo::flag);
return *Foo::instance;
}
Foo .
, - , . boost:: once ( : beware), , -, boost::, :: bind .
To remove Singleton, you can create boost :: shared_ptr at the compilation unit level and bind your pointer to it and use a custom remote element that is a static member of your class so that the removal can remain private. Your sender will be able to call deletion, though, and your init function will have access to the deleter function (which is also private) to initialize shared_ptr with it.
source
share