tbwshc

tbw

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  95 Posts :: 8 Stories :: 3 Comments :: 0 Trackbacks

常用链接

留言簿(4)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

(转载)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();
}

posted on 2012-08-12 12:48 tbwshc 阅读(1445) 评论(0)  编辑 收藏 引用