#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class singleton
{
public:
static singleton* getInstance();
int getValue();
void setValue(int iValue);
private:
int value;
singleton();//私有构造函数
~singleton();//析构函数
};
singleton::singleton()//构造函数
{
cout << "构造函数执行" << endl;
}
singleton::~singleton()//析构函数
{
cout << "析构函数执行" << endl;
}
singleton* singleton::getInstance()
{
static singleton single;
return &single;
}
int singleton::getValue()
{
return value;
}
void singleton::setValue(int v)
{
value = v;
}
int main()
{
cout << "单件模式" << endl;
singleton* p1 = singleton::getInstance();
singleton* p2 = singleton::getInstance();
p1->setValue(123);
if (p1->getValue() == p2->getValue())
{
cout << "相同" << endl;
}
else
{
cout << "不相同" << endl;
}
return 0;
}