Redian新闻
>
FIA SPG都有了 接下来搞什么好
avatar
FIA SPG都有了 接下来搞什么好# Money - 海外理财
a*2
1
如果singleton pattern的getInstance()方法定义成这样有问题吗?
class A
{
public:
static A* getInstance()
{
static A a;
return &a;
}
};
avatar
w*n
2
求大神们指点
才几个月的记录
Citi FIA SPG都搞了 接下来搞个啥好 discover or freedom or sapphire?
avatar
j*c
3
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;
: }
: };

avatar
b*u
4
休息一阵吧。
[发表自未名空间手机版 - m.mitbbs.com]
avatar
h*3
5
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;
: }
: };

avatar
i*g
6
sigh 常逛本版免不了过几天就想申新卡

求大神们指点
才几个月的记录
Citi FIA SPG都搞了 接下来搞个啥好 discover or freedom or sapphire?

【在 w*****n 的大作中提到】
: 求大神们指点
: 才几个月的记录
: Citi FIA SPG都搞了 接下来搞个啥好 discover or freedom or sapphire?

avatar
v*7
7
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.
avatar
d*g
8
discover and freedom
avatar
c*n
9
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;
: }
: };

avatar
g*y
10
觉得最近的新手们都很猛啊!
老了,不行了,跳不动了……
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。