- 单例模式:一个类只有一个实例化对象。
- 第一步:构造函数,拷贝构造函数私有,再添加一个静态实例化指针也是私有
- 第二步:类外静态对象指针实例化
- 第三步:创建一个能够获取实例化指针的接口
- 第四步:通过接口获取唯一的实例化对象
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class father
{
public:
//第三步:创建一个能够获取实例化指针的接口
static father *getinstance()
{
return m_f;
}
private:
//第一步:构造函数,拷贝构造函数私有,再添加一个静态实例化指针也是私有
father() {};
father(const father & f) {};
static father * m_f;
};
//第二部类外静态对象指针实例化
father* father::m_f = new father;
int main()
{
//第四步,通过接口获取实例化对象
father *p1 = father::getinstance();
father *p2 = father::getinstance();
system("pause");
return EXIT_SUCCESS;
}