首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

单件形式Singleton的实现

2013-03-06 
单件模式Singleton的实现#includeiostreamusing namespace std class Singleton{private :static Singl

单件模式Singleton的实现

#include<iostream>

using namespace std;
 class Singleton
{
private :
static Singleton* singleton;
Singleton()
{
cout<<"This is the Singleton";
}
public:
static Singleton* getSingleton()
{
if(singleton==0)
{
singleton=new Singleton;
}
return singleton;
}
};

 void main()
 {
 Singleton *s;
 s=Singleton::getSingleton();
 }


以上代码链接的时候出现错误,不知道是什么原因
[解决办法]

#include<iostream>
 
using namespace std;
 class Singleton
{
private :
    static Singleton* singleton;
    Singleton()
    {
        cout<<"This is the Singleton";
    }
public:
    static Singleton* getSingleton()
    {
        if(singleton==0)
        {
        singleton=new Singleton;
        }
        return singleton;
    }
};

 Singleton* Singleton::singleton = NULL;

 void main()
 {
     Singleton *s;
     s=Singleton::getSingleton();
 }

[解决办法]
C++类静态成员的初始化 
http://blog.csdn.net/jakiechen68/article/details/7288225

热点排行