- #include <iostream>
-
using namespace std;
-
-
class Singleton {
-
public:
-
static Singleton *getInstance()
-
{
-
if (instance == 0)
-
instance = new Singleton();
-
return instance;
-
}
-
-
void printHello()
-
{
-
cout << "Hello" << endl;
-
}
-
-
private:
-
static Singleton *instance;
-
Singleton() {cout << "Init a instance" << endl;}
-
};
-
-
Singleton* Singleton::instance = 0;
-
-
int
-
main(void)
-
{
-
int i;
-
for (i = 0; i < 10; i++)
-
Singleton::getInstance()->printHello();
-
-
return 0;
- }
- 一个实例
- 全局访问