Chinaunix首页 | 论坛 | 博客
  • 博客访问: 358474
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-05-17 09:43:02

  1. #include <iostream>
  2. using namespace std;

  3. class Singleton {
  4. public:
  5.     static Singleton *getInstance()
  6.     {
  7.         if (instance == 0)
  8.             instance = new Singleton();
  9.         return instance;
  10.     }
  11.     
  12.     void printHello()
  13.     {
  14.         cout << "Hello" << endl;
  15.     }

  16. private:
  17.     static Singleton *instance;
  18.     Singleton() {cout << "Init a instance" << endl;}
  19. };

  20. Singleton* Singleton::instance = 0;

  21. int
  22. main(void)
  23. {
  24.     int i;
  25.     for (i = 0; i < 10; i++)
  26.         Singleton::getInstance()->printHello();

  27.     return 0;
  28. }
  • 一个实例
  • 全局访问



阅读(1276) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~