C++的Singleton模式实现
2011-04-05 10:16
316 查看
Singleton.h
class A {
private:
static A *_instance;
protected:
A();
public:
static A* getInstance();
void sayhello();
};
Singleton.cpp
#include <iostream>
#include "singleton.h"
using namespace std;
A* A::_instance = 0;
A::A() {}
A* A::getInstance() {
if (_instance == 0)
_instance = new A;
return _instance;
}
void A::sayhello() {
cout << "Hello!" << endl;
}
testsingleton.cpp
#include "singleton.h"
#include <iostream>
using namespace std;
int main()
{
A *abc = A::getInstance();
abc->sayhello();
return 0;
}
class A {
private:
static A *_instance;
protected:
A();
public:
static A* getInstance();
void sayhello();
};
Singleton.cpp
#include <iostream>
#include "singleton.h"
using namespace std;
A* A::_instance = 0;
A::A() {}
A* A::getInstance() {
if (_instance == 0)
_instance = new A;
return _instance;
}
void A::sayhello() {
cout << "Hello!" << endl;
}
testsingleton.cpp
#include "singleton.h"
#include <iostream>
using namespace std;
int main()
{
A *abc = A::getInstance();
abc->sayhello();
return 0;
}
相关文章推荐
- C++实现Creational - Singleton模式
- Head First设计模式C++实现-Singleton模式
- C++完美实现Singleton模式zz
- 剑指offer 面试题2 Singleton模式 C++实现
- Head First设计模式C++实现-Singleton模式
- C++完美实现Singleton模式
- 模板singleton模式的C++实现
- C++实现Creational - Singleton模式
- C++实现Singleton模式
- C++完美实现Singleton模式
- C++完美实现Singleton模式
- Singleton模式Linux下的C++实现
- C++完美实现Singleton模式
- 每天一题(48) - C++实现Singleton模式
- C++实现Singleton模式
- C++完美实现Singleton模式[转]
- 每天一题(48) - C++实现Singleton模式
- C++完美实现Singleton模式
- 设计模式的解析和实现(C++)之五-Singleton模式
- CSDN技术中心 Singleton模式的C++实现研究(转载)