C++單件模式實(shí)現(xiàn)代碼詳解
作者:佚名 
  我們今天將會(huì)通過(guò)一段代碼的方式為大家詳細(xì)介紹C++單件模式的實(shí)現(xiàn)方法。希望初學(xué)者們可以通過(guò)這里介紹的內(nèi)容充分掌握這一應(yīng)用技術(shù)。
 在C++這樣一款功能強(qiáng)大的計(jì)算機(jī)編程語(yǔ)言中,有很多比較復(fù)雜的功能,需要我們?cè)诓粩嗟膶?shí)踐中去積累經(jīng)驗(yàn),理清這些功能的應(yīng)用特點(diǎn)。在這里我們就先來(lái)了解一下C++單件模式的相關(guān)實(shí)現(xiàn)方式。
C++單件模式代碼示例:
- class Singleton
 - {
 - public:
 - static Singleton * Instance()
 - {
 - if( 0== _instance)
 - {
 - _instance = new Singleton;
 - }
 - return _instance;
 - }
 - protected:
 - Singleton(){}
 - virtual ~Singleton(void){}
 - static Singleton* _instance;
 - };
 
2) 利用智能指針進(jìn)行垃圾回收
- class Singleton
 - {
 - public:
 - ~Singleton(){}
 - static Singleton* Instance()
 - {
 - if(!pInstance.get())
 - {
 - pInstance = std::auto_ptr<Singleton>(new Singleton());
 - }
 - return pInstance.get();
 - }
 - protected:
 - Singleton(){}
 - private:
 - static std::auto_ptr<Singleton> pInstance;
 - };
 
以上就是對(duì)C++單件模式的相關(guān)操作步驟。
【編輯推薦】
責(zé)任編輯:曹凱 
                    來(lái)源:
                    博客園
 














 
 
 





 
 
 
 