你能否真正搞定单例?
2014-03-16 00:27
155 查看
笔试面试让写个单例,不一定每个人都能搞出来。我们以前也谈论过单例,现在继续来看看:
可以写为:
但是,下面有错:
下面也有错:
奇葩的是,下面也有错:
你需要写成如下才好啊:
当然,如果你愿意,不用成员变量,而是用成员函数的局部静态变量或者该文件的全局静态变量表示单例,也是可以的。 多尝试,多观察,多总结吧。 我始终觉得,搞得面试,很少人能完全一次搞定这些,我在其内。
可以写为:
#include <iostream> using namespace std; class A { private: int x; static A *pInstance; public: void set(int m) { x = m; } int get() { return x; } static A *getInstance(); }; A * A::getInstance() { static A *pInstance = NULL; if(NULL == pInstance) { pInstance = new A; } return pInstance; } int main() { A *p1 = A::getInstance(); A *p2 = A::getInstance(); if(p1 == p2) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }
但是,下面有错:
#include <iostream> using namespace std; class A { private: int x; static A *pInstance; public: void set(int m) { x = m; } int get() { return x; } static A *getInstance(); }; static A * A::getInstance() { static A *pInstance = NULL; if(NULL == pInstance) { pInstance = new A; } return pInstance; } int main() { A *p1 = A::getInstance(); A *p2 = A::getInstance(); if(p1 == p2) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }
下面也有错:
#include <iostream> using namespace std; class A { private: int x; static A *pInstance; public: void set(int m) { x = m; } int get() { return x; } static A *getInstance(); }; A * A::getInstance() { pInstance = NULL; if(NULL == pInstance) { pInstance = new A; } return pInstance; } int main() { A *p1 = A::getInstance(); A *p2 = A::getInstance(); if(p1 == p2) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }
奇葩的是,下面也有错:
#include <iostream> using namespace std; class A { private: int x; static A *pInstance; public: void set(int m) { x = m; } int get() { return x; } static A *getInstance() { pInstance = NULL; if(NULL == pInstance) { pInstance = new A; } return pInstance; } }; int main() { A *p1 = A::getInstance(); A *p2 = A::getInstance(); if(p1 == p2) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }
你需要写成如下才好啊:
#include <iostream> using namespace std; class A { private: int x; public: void set(int m) { x = m; } int get() { return x; } static A *getInstance() { static A* pInstance = NULL; if(NULL == pInstance) { pInstance = new A; } return pInstance; } }; int main() { A *p1 = A::getInstance(); A *p2 = A::getInstance(); if(p1 == p2) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }
当然,如果你愿意,不用成员变量,而是用成员函数的局部静态变量或者该文件的全局静态变量表示单例,也是可以的。 多尝试,多观察,多总结吧。 我始终觉得,搞得面试,很少人能完全一次搞定这些,我在其内。
相关文章推荐
- 话题讨论:Facebook能否成为真正的王者?
- 话题讨论:Facebook能否成为真正的王者?
- 一篇网络文章,能否让我真正顿悟???
- 往往客户的需求是逐渐被深入的真正的实际需求往往没刚开始所说的那么简单容易就可以搞定
- 话题讨论:Facebook能否成为真正的王者?
- 往往客户的需求是逐渐被深入的真正的实际需求往往没刚开始所说的那么简单容易就可以搞定
- 往往客户的需求是逐渐被深入的真正的实际需求往往没刚开始所说的那么简单容易就可以搞定
- 当C2C在淘宝衰落之后,“小交易”能否用“关系链+地理位置”让真正意义上的C2C复兴?
- 真正技术不是一两天就能搞定的,需要长年累月的沉淀;自信也是如此
- 分簇技术能否真正拯救RAID存储?
- 云安全能否给企业带来真正的安全?
- 小尺寸屏幕能否解决Windows平板电脑的真正问题
- 【C#】Json转换Xml真的有那么麻烦吗?用两句话真正搞定Json转Xml
- 往往客户的需求是逐渐被深入的真正的实际需求往往没刚开始所说的那么简单容易就可以搞定
- JFreeChart 中文乱码 一站式解决 一个方法搞定
- 封装一个类搞定90%安卓客户端与服务器端交互
- 无需软件,笔记本自带wifi功能,几条命令轻松搞定
- oracle dblink两步搞定Oracle两个数据库间的表同步
- 新年新气象 给大家分享一篇真正提升水平的攻略
- Stay Hungry Stay Foolish的真正含义