(转载)C++教程网www.cppcourse.com
这是一个C++单件模式板类的一种实现,这个类的实现有别于传统的用继承或者宏的方式来实现。
这里的singleton_holder实际上是类的包装器。
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();
}