(轉(zhuǎn)載)C++教程網(wǎng)www.cppcourse.com
這是一個C++單件模式板類的一種實(shí)現(xiàn),這個類的實(shí)現(xiàn)有別于傳統(tǒng)的用繼承或者宏的方式來實(shí)現(xiàn)。
這里的singleton_holder實(shí)際上是類的包裝器。
template <typename T>class singleton_holder{public: typedef T obj_type;
static T& instance()
{ static T obj;
return obj;
} private: singleton_holder();
singleton_holder(
const singleton_holder& s);
singleton_holder& operator=(
const singleton_holder& s);
~singleton_holder();
};
class application_impl
{
public:
void run()
{
std::cout<<“this is a testb”<<std::endl;
}
}
typedef singleton_holder<application_impl> application;
void main()
{
application::obj_type& app = application::instance();
app.run();
}