C++ 使用模板 实现单例模式

template
class Singleton{
public:
    static T* getInstance(){
        if(ptr==NULL){
            ptr=new T();//(T*)(::operator new(sizeof(T)));
        }
        return ptr;
    }
private:
    Singleton(){};
    static T* ptr;
};
template
T* Singleton::ptr=0;

class C{
public:
    int x;
    C(){
        x=0;
    }
    ~C(){
        cout<<"C delete"<::getInstance();
    C* d=Singleton::getInstance();

    cout<


你可能感兴趣的:(C/C++)