由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - singleton pattern problem
相关主题
关于singleton 的面试题C++ Singleton的实现
请问一道special singleton class的题C++ Singleton Template - 编译通不过
面试常考哪些java的design pattern弱问:singleton要不要destructor啊?
static initialization dependency c++ (转载)Amazon电面 (05/02/09更新第二次,第三次电面)
Google电面被拒,郁闷中google面试的多线程问题
gg面试题攒rp,电面题目
Can a 10-year-Java guy answer these 2 questions promptly?一个电面
问一个thread safe singleton的问题问个static STL container的问题
相关话题的讨论汇总
话题: singleton话题: static话题: pattern话题: instance
进入JobHunting版参与讨论
1 (共1页)
a**********2
发帖数: 340
1
如果singleton pattern的getInstance()方法定义成这样有问题吗?
class A
{
public:
static A* getInstance()
{
static A a;
return &a;
}
};
j******c
发帖数: 294
2
This is wrong. google singleton, you should get a lot of correct sample
codes.

【在 a**********2 的大作中提到】
: 如果singleton pattern的getInstance()方法定义成这样有问题吗?
: class A
: {
: public:
: static A* getInstance()
: {
: static A a;
: return &a;
: }
: };

h******3
发帖数: 351
3
at least the definition of
static A a
should be put under class instead of public method


【在 a**********2 的大作中提到】
: 如果singleton pattern的getInstance()方法定义成这样有问题吗?
: class A
: {
: public:
: static A* getInstance()
: {
: static A a;
: return &a;
: }
: };

v*******7
发帖数: 187
4
I forgot, but some how it should be like this:
class Singleton{
public:
static Singleton * getInstance() {
if (_instance == NULL) {
_instance = new Singleton;
}
return _instance;
}

private:
static Singleton _instance;
Singleton();
~Singleton();
}
I forgot the details, but I think the above is the main idea of Singleton.
c******n
发帖数: 4965
5
I don't see a problem with this
if you compile this code with gcc, and then decompile the resulting
code, the assembley looks like:
lock(some_lock)
if ( not_initialized ) {
a = new A();
not_initialized = false;
}
unlock(some_lock)
in fact, for the java version, the book "effective java" actually
recommends the idiom
class A {
static A ins = new A();
static A getInstance() { return ins;}
}
the java version has a bigger problem though: you can't control when
the ins is initialized, thus agains the goal of "lazy initialization"
of singleton pattern

【在 a**********2 的大作中提到】
: 如果singleton pattern的getInstance()方法定义成这样有问题吗?
: class A
: {
: public:
: static A* getInstance()
: {
: static A a;
: return &a;
: }
: };

1 (共1页)
进入JobHunting版参与讨论
相关主题
问个static STL container的问题Google电面被拒,郁闷中
c++ interview questiongg面试题
这种牛逼的写法是实现Map接口的匿名内部类吗?Can a 10-year-Java guy answer these 2 questions promptly?
Java/C++ 的牛人们给看看这个Interview的Home test (急)问一个thread safe singleton的问题
关于singleton 的面试题C++ Singleton的实现
请问一道special singleton class的题C++ Singleton Template - 编译通不过
面试常考哪些java的design pattern弱问:singleton要不要destructor啊?
static initialization dependency c++ (转载)Amazon电面 (05/02/09更新第二次,第三次电面)
相关话题的讨论汇总
话题: singleton话题: static话题: pattern话题: instance