C++单例小例子
#pragma once
class CDoctor
{
private:
CDoctor(void);
public:
~CDoctor(void);
static CDoctor* Instance();
public:
void Display();
private:
static CDoctor* instance;
};
#include "Doctor.h"
#include <iostream>
using namespace std;
CDoctor::CDoctor(void)
{
}
CDoctor::~CDoctor(void)
{
}
CDoctor *CDoctor::instance = NULL;
CDoctor* CDoctor::Instance()
{
if(NULL == instance)
instance = new CDoctor();
return instance;
}
void CDoctor::Display()
{
cout << "Doctor Display" << endl;
}
#include <string>
#include "Doctor.h"
#include <iostream>
using namespace std;
void main()
{
string welcome = "hello world";
cout << welcome << endl;
CDoctor::Instance()->Display();
int wait;
cin >> wait ;
}