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

单例模式

时间:2015-04-27 12:42:57      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

   单例模式:确保一个类只有一个实例,并提供全局访问点。——《HEAD FIRST 设计模式》

  我的c++代码:

#ifndef DESIGN_SINGLETON_H_
#define DESIGN_SINGLETON_H_

#include <iostream>

class Singleton
{
private:
    Singleton(){}
public:
    static Singleton* GetInstance();

private:
    static Singleton* g_instance;
};

#endif // DESIGN_SINGLETON_H_
#include "singleton.h"

Singleton* Singleton::g_instance = new Singleton();

Singleton* Singleton::GetInstance()
{
    if (g_instance == 0)
    {
        std::cout << "singleton initing..." << std::endl;
        g_instance = new Singleton();
    }

    return g_instance;
}
       个人感悟:待留。

单例模式

标签:

原文地址:http://www.cnblogs.com/foolbread/p/4459741.html

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