C++设计模式(二)单例模式
来源:互联网 发布:淘宝品控申诉 编辑:程序博客网 时间:2024/06/05 18:54
参考C++设计模式这本书,写了一个示例,如下:
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;/* 单例 */class Singleton{private: static Singleton* m_pInstance; int m_data;protected: Singleton():m_data(0) { cout << "Singleton mode" << endl; }public: static Singleton* getInstance() { if(m_pInstance == NULL) { m_pInstance = new Singleton(); } return m_pInstance; } void setData(int d) { m_data = d; } int getData() { return m_data; }};Singleton* Singleton::m_pInstance = NULL;int main(void){ Singleton* p = Singleton::getInstance(); p->setData(10); cout << "data = " << p->getData() << endl; Singleton* p1 = Singleton::getInstance(); // p1 = p cout << "data = " << p1->getData() << endl; return 0;}
0 0
- 模式设计(二)------单例模式
- 设计模式(二)--单例模式
- 设计模式(二)单例模式
- 设计模式(二):单例模式
- 设计模式(二)单例模式
- 设计模式(C++) -----单例模式
- 设计模式(二)单例设计模式
- Java单例设计模式(二)
- 设计模式之二--- 单例模式
- C++设计模式[二]单例模式
- 【设计模式】二、单例模式
- 设计模式(二)-------->单例模式
- 设计模式之单例模式(二)
- 二、设计模式之一-----单例模式
- 【设计模式 二】单例模式
- c++设计模式二,单例模式
- 设计模式<二>单例模式
- 设计模式<二>单例模式
- Make 命令教程详解(转自阮一峰的个人博客)
- linux目录与文件的权限意义
- 中国人对科学的认识误区:从李约瑟难题到钱学森之问
- 机房重构——数据库
- iOS讲解迷惑--LeanCloud
- C++设计模式(二)单例模式
- 一张图说懂java中 private default protected public 的区别
- eclipse, Log4j配置(真心的详细~)
- git reset的三种模式
- PHP
- HDU 2918 Tobo or not Tobo
- poj 2184 Cow Exhibition dp
- POJ3292Semi-prime H-numbers(筛选法)
- js获取网站绝对路径