单例模式: 保证一个类仅有一个实例,并提供一个访问它的全局访问点。

#include  <iostream>
using namespace std ;

template 
<class T>
class ST{
public:
    
static T*  GetInstance() {
        
if(m_pInstance == NULL){
            m_pInstance 
= new T ;
        }
        
return m_pInstance ;
    }
protected:
    
static T* m_pInstance ;

};

/// \模板静态成员初始化
template <class T>
*  ST< T>::m_pInstance = (T*)NULL ;


class AB
{
public:
    AB(){

    }
    
~AB(){

    }
    
virtual void Init(){
        cout
<<"AB::Init" <<endl;
        
return ;
    }
};

int main(){

    
/// \TEST
    //ST<AB> ab ;
    AB* pab ;
    pab 
= ST<AB>::GetInstance() ;
    
// pab = ab.GetInstance() ;
    pab->Init() ;

    
return 0 ;
}
posted on 2013-07-31 22:28 Fanze 阅读(1433) 评论(0)  编辑 收藏 引用 所属分类: 设计模式

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理