码迷,mamicode.com
首页 > 其他好文 > 详细

Singleton Pattern

时间:2015-05-20 23:47:23      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

Singleton.h

#ifndef _SINGLETON_H
#define _SINGLETON_H

class Singleton {
public :
    static Singleton *Instance();
protected :
    Singleton();
private:
    static Singleton *_instance;
};

#endif

Singleton.cpp

#include "Singleton.h"
#include <iostream>
using namespace std;

Singleton *Singleton::_instance = 0;

Singleton::Singleton() {
    cout << "Singleton..." << endl;
}

Singleton *Singleton::Instance() {
    if (_instance == 0) {
        _instance = new Singleton();
    }
    return _instance;
}

Main.cpp

#include "Singleton.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {

    Singleton *sgn = Singleton::Instance();

    return 0;
}

Singleton Pattern

标签:

原文地址:http://www.cnblogs.com/Susake/p/4518494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!