1. 程式人生 > >c++單例模式

c++單例模式

ptr pan 單例 get pri 模式 on() public clas

自己寫的單例模式

class singleton{
public:
    
    singleton(const singleton&) = delete;
    singleton& operator=(const singleton& rhs) = delete;
    static singleton* getinstance(){
        if (ptr == NULL) {
            ptr = new singleton(); return ptr;
        }
        else return NULL;
    }
    
int geta(){ return a; } private: static singleton* ptr; singleton(){ a = 5; } int a; }; Singleton* Singleton::ptr = NULL;

c++單例模式