由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Code for Singleton design pattern
相关主题
问一道c++面试题singleton pattern problem
Google电面被拒,郁闷中Palantir 2nd coding interview [pass, set for on-site]
gg面试题请问一道special singleton class的题
C++ Singleton的实现Can a 10-year-Java guy answer these 2 questions promptly?
问个c++的问题被鄙视了, c++基础知识
关于singleton 的面试题刚才有个讨论singleton的帖子,找不到了
singleton哪种写法好?问一个thread safe singleton的问题
C++ Singleton Template - 编译通不过面试常考哪些java的design pattern
相关话题的讨论汇总
话题: myclass话题: singleton话题: static话题: code
进入JobHunting版参与讨论
1 (共1页)
c**z
发帖数: 669
1
not sure where is wrong? Can someone help?
Thanks
class myclass
{
private:
myclass() {}
static myclass uniqueinstance;
static bool b;
public:
static myclass getinstance()
{
if(b==false)
{uniqueinstance=myclass();
b=true;
}
return uniqueinstance;
}
};
bool myclass::b=false;
void main()
{
myclass.getinstance();
}
a********1
发帖数: 750
2
至少copy constructor 和assignment operator 也要private吧
P**********c
发帖数: 3417
3
re. 另外这个不thread safe。这种情况一般应该用double-checking lock.

【在 a********1 的大作中提到】
: 至少copy constructor 和assignment operator 也要private吧
r*******y
发帖数: 1081
4
it is not OK to return a copy of the object in the singleton pattern.
need to return a reference or a pointer

【在 c**z 的大作中提到】
: not sure where is wrong? Can someone help?
: Thanks
: class myclass
: {
: private:
: myclass() {}
: static myclass uniqueinstance;
: static bool b;
: public:
: static myclass getinstance()

l****l
发帖数: 8
5
myclass.getinstance(); => myclass::getinstance();

【在 c**z 的大作中提到】
: not sure where is wrong? Can someone help?
: Thanks
: class myclass
: {
: private:
: myclass() {}
: static myclass uniqueinstance;
: static bool b;
: public:
: static myclass getinstance()

r*********2
发帖数: 88
6
double check locking pattern.
1 (共1页)
进入JobHunting版参与讨论
相关主题
面试常考哪些java的design pattern问个c++的问题
Bloomberg电面面经关于singleton 的面试题
Careercup 设计题Parking Lot讨论singleton哪种写法好?
贴一个电梯设计伪码吧。C++ Singleton Template - 编译通不过
问一道c++面试题singleton pattern problem
Google电面被拒,郁闷中Palantir 2nd coding interview [pass, set for on-site]
gg面试题请问一道special singleton class的题
C++ Singleton的实现Can a 10-year-Java guy answer these 2 questions promptly?
相关话题的讨论汇总
话题: myclass话题: singleton话题: static话题: code