g*********s 发帖数: 1782 | 1 1. the default ctor is declared as private. but how about its copy ctor,
assignment, and dtor?
2. is the method of get_instance() required to be static? |
e****d 发帖数: 895 | 2 no public default constructor and public copy constructor.
get_instance() is static member function.
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
n**f 发帖数: 121 | 3 1. all private. if not sure, add assertions
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
e****d 发帖数: 895 | 4 copy assignment and destructor don't need to be private.
【在 n**f 的大作中提到】 : 1. all private. if not sure, add assertions
|
g*********s 发帖数: 1782 | 5
u haven't answered: is it required to be static? if so, why?
【在 e****d 的大作中提到】 : no public default constructor and public copy constructor. : get_instance() is static member function.
|
g*********s 发帖数: 1782 | 6 but is it preferred to be private or not?
【在 e****d 的大作中提到】 : copy assignment and destructor don't need to be private.
|
w***g 发帖数: 5958 | 7 不是static的你得有一个对象才能调用吧. single你在调用get_instance之前又不可能
有这个类的对象, 就只好static了.
【在 g*********s 的大作中提到】 : but is it preferred to be private or not?
|
p*****d 发帖数: 80 | 8 singleton
1. ctor: 必须private
否则会允许出现多个instance,这个和singleton的定义矛盾。而且必须定义无参数的ctor,否则编译器默认会定义一个。
2. get_instance method:必须static
否则无法在没有singleton instance的情况下调用产生第一个也是唯一一个instance。
3. copy ctor:必须private
否则下面的语句会出现两个instance
S *s = S::get_instance();
S s1(*s);
4. assignment optr:不必private
因为不会出现第二个instance,所以编译器默认生成的assignment operator只能自己assign自己,无效果。
5. dtor:看情况
dtor可能是有用的,如果允许用户自己释放singleton的话,因为singleton的定义
是无论何时在系统中最多有一个instance存在,可以是1个也可以是0个。但通常是定义一个release函数来free分配的内存而不是使用dtor。但即使dtor是public的,系统也不会去调用dtor,所以始终在程序不需要singleton的时候用release进行清理是好的习惯。
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
w***g 发帖数: 5958 | 9 赞牛人. 做学问做到这个地步就比较值得尊敬了.
ctor,否则编译器默认会定义一个。
【在 p*****d 的大作中提到】 : singleton : 1. ctor: 必须private : 否则会允许出现多个instance,这个和singleton的定义矛盾。而且必须定义无参数的ctor,否则编译器默认会定义一个。 : 2. get_instance method:必须static : 否则无法在没有singleton instance的情况下调用产生第一个也是唯一一个instance。 : 3. copy ctor:必须private : 否则下面的语句会出现两个instance : S *s = S::get_instance(); : S s1(*s); : 4. assignment optr:不必private
|
g*********s 发帖数: 1782 | 10 how about:
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
的ctor,
否则编译器默认会定义一个。
【在 p*****d 的大作中提到】 : singleton : 1. ctor: 必须private : 否则会允许出现多个instance,这个和singleton的定义矛盾。而且必须定义无参数的ctor,否则编译器默认会定义一个。 : 2. get_instance method:必须static : 否则无法在没有singleton instance的情况下调用产生第一个也是唯一一个instance。 : 3. copy ctor:必须private : 否则下面的语句会出现两个instance : S *s = S::get_instance(); : S s1(*s); : 4. assignment optr:不必private
|
|
|
w***g 发帖数: 5958 | 11 你这个自己做系统可以, 去面试肯定通不过的(我没学过DP, 曾经在面试时给出过你这
个答案. 这个太显然了,面试官不会满意的). 你要按playcod的答案去面试, 肯定无
往而不利.
【在 g*********s 的大作中提到】 : how about: : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : { : return &_x;
|
g*********s 发帖数: 1782 | 12 unfortunately, this is the example the interviewer showing to me.
he asks if get_instance() is required to be static and if it's required to
declare the singleton as a pointer instead of object.
【在 w***g 的大作中提到】 : 你这个自己做系统可以, 去面试肯定通不过的(我没学过DP, 曾经在面试时给出过你这 : 个答案. 这个太显然了,面试官不会满意的). 你要按playcod的答案去面试, 肯定无 : 往而不利.
|
e****d 发帖数: 895 | 13 how do you call get_X() ?
【在 g*********s 的大作中提到】 : how about: : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : { : return &_x;
|
g*********s 发帖数: 1782 | 14
yes, that's the point i missed.
【在 e****d 的大作中提到】 : how do you call get_X() ?
|
e****d 发帖数: 895 | 15 so, you know you cannot call a non-static member function
without an object?
【在 g*********s 的大作中提到】 : : yes, that's the point i missed.
|
p*****d 发帖数: 80 | 16 如果你的代码里的get_X改成static,在理论上是可以的。
我说的那五条里面只有最后一条需要改:dtor必须是private的,而且不能有release函
数,因为在singleton中可能会有其他的member是class,如果dtor被调用会出现double
free的问题,也就是说一些资源在s->~S()时被free了一次,最后调用s_instance的
dtor时又会被free一次。
但是,这不是好的做法,因为你失去了对资源何时建立以及释放的控制权。static S s
_instance在程序开始前被调用的问题有很多:比如初始化其中资源可能依赖于其他的
数据,而程序开始前这些数据尚未准备好,所以初始化这些资源是没有意义的;另外一
个问题是static变量的初始化顺序并不统一,理论上讲是header file被include的顺序
,但各个编译器实现并不一样,因此如果多个static的instance初始化不独立的话,这
会带来undetermined behavior,所以要尽可能避免class的静态instance。
【在 g*********s 的大作中提到】 : how about: : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : { : return &_x;
|
e****d 发帖数: 895 | 17 Calling dtor twice has nothing to do with singleton.
The same thing applies to calling dtor explicitly for
block scope objects.
double
s
【在 p*****d 的大作中提到】 : 如果你的代码里的get_X改成static,在理论上是可以的。 : 我说的那五条里面只有最后一条需要改:dtor必须是private的,而且不能有release函 : 数,因为在singleton中可能会有其他的member是class,如果dtor被调用会出现double : free的问题,也就是说一些资源在s->~S()时被free了一次,最后调用s_instance的 : dtor时又会被free一次。 : 但是,这不是好的做法,因为你失去了对资源何时建立以及释放的控制权。static S s : _instance在程序开始前被调用的问题有很多:比如初始化其中资源可能依赖于其他的 : 数据,而程序开始前这些数据尚未准备好,所以初始化这些资源是没有意义的;另外一 : 个问题是static变量的初始化顺序并不统一,理论上讲是header file被include的顺序 : ,但各个编译器实现并不一样,因此如果多个static的instance初始化不独立的话,这
|
p*****d 发帖数: 80 | 18 是和singleton的定义没关系,所以我说的是编程习惯的问题,要考虑到用户可能误用
,应该在compile time杜绝这种可能,但如果s_instance是pointer则dtor/release是
feature而非bug,这就是区别所在。
【在 e****d 的大作中提到】 : Calling dtor twice has nothing to do with singleton. : The same thing applies to calling dtor explicitly for : block scope objects. : : double : s
|
C**m 发帖数: 126 | 19 #include
#include
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
int main(int argc, char* argv[])
{
X *p;
std::cout << "the address " << (void *) p->get_X() << std::endl;
X a;
std::cout << "the address " << (void *) a.get_X() << std::endl;;
X b;
std::cout << "the address " << (void *) b.get_X() << std::endl;;
return 0;
}
/* the output is
* gcc 4.4.5
the address 0x804a180
the address 0x804a180
the address 0x804a180
*/ |
l*********s 发帖数: 5409 | 20 but that is not much different. You instantiate multiple wrapper objects and
all of them hold the unique static true object. right?
【在 e****d 的大作中提到】 : so, you know you cannot call a non-static member function : without an object?
|
|
|
t****t 发帖数: 6806 | 21 why do you want "wrapper objects" in the first place?
and
【在 l*********s 的大作中提到】 : but that is not much different. You instantiate multiple wrapper objects and : all of them hold the unique static true object. right?
|
l*********s 发帖数: 5409 | 22 jut wonder if this is an answer for the interview question.
【在 t****t 的大作中提到】 : why do you want "wrapper objects" in the first place? : : and
|
e****d 发帖数: 895 | 23 not sure how your p->get_X() can go through. p is not initialized
and applying -> operator on it should cause undefined behavor.
【在 C**m 的大作中提到】 : #include : #include : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : {
|
d****p 发帖数: 685 | 24 This should be fine as long as the this pointer is not used, which is the
case in his code.
【在 e****d 的大作中提到】 : not sure how your p->get_X() can go through. p is not initialized : and applying -> operator on it should cause undefined behavor.
|
t****t 发帖数: 6806 | 25 it's undefined, although in most cases you can run it.
【在 d****p 的大作中提到】 : This should be fine as long as the this pointer is not used, which is the : case in his code.
|
e****d 发帖数: 895 | 26 Since get_X() is a non-static member function, p is passed as "this" into
get_X(). How do you define whether a pointer is used or not in this case? |
d****p 发帖数: 685 | 27
In case p is uninitialized, what's wrong with
get_X(p) when get_X doesn't reference p? get_X clearly not a virtual
function. In this case get_X is actually a static member function which
takes in a redundant parameter.
Previously static member function was implemented as ((*)0)->mem
_func.
【在 e****d 的大作中提到】 : Since get_X() is a non-static member function, p is passed as "this" into : get_X(). How do you define whether a pointer is used or not in this case?
|
e****d 发帖数: 895 | 28 you are expecting the compiler does exactly what you intend to do.
However, the c++ spec says dereferecning a null/uninitialized
pointer has undefined behavior. Therefore, even your code works
in one compiler, it doesn't mean to work in other compilers.
mem
【在 d****p 的大作中提到】 : : In case p is uninitialized, what's wrong with : get_X(p) when get_X doesn't reference p? get_X clearly not a virtual : function. In this case get_X is actually a static member function which : takes in a redundant parameter. : Previously static member function was implemented as ((*)0)->mem : _func.
|
d****p 发帖数: 685 | 29
This is true.
However, in the case we discussed, NO compiler will ever deference the this
pointer.
【在 e****d 的大作中提到】 : you are expecting the compiler does exactly what you intend to do. : However, the c++ spec says dereferecning a null/uninitialized : pointer has undefined behavior. Therefore, even your code works : in one compiler, it doesn't mean to work in other compilers. : : mem
|
p*****d 发帖数: 80 | 30 这个例子太特殊了,没代表性,通常singleton中会有其他的数据,你给X加个其他
member就不行了。
【在 C**m 的大作中提到】 : #include : #include : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : {
|
|
|
h****e 发帖数: 2125 | 31
why can't? try this:
"
#include
class X
{
public:
X* getInstance() { return &_x; }
void setMember( int i ) { _i = i; }
int getMember() const { return _i; }
const int* getMemberAddr() const { return (long long)&_i; }
private:
static X _x;
int _i;
};
X X::_x;
int main()
{
X* p;
std::cout << p->getInstance() << std::endl;
p->getInstance()->setMember( 10 );
std::cout << p->getInstance()->getMember() << std::endl;
std::cout << p->getInstance()->getMemberAddr() << std::endl;
X* q;
std::cout << q->getInstance()->getMember() << std::endl;
std::cout << q->getInstance()->getMemberAddr() << std::endl;
}
"
【在 p*****d 的大作中提到】 : 这个例子太特殊了,没代表性,通常singleton中会有其他的数据,你给X加个其他 : member就不行了。
|
p*****d 发帖数: 80 | 32 用ctor定义pq,会有两份_i。
【在 h****e 的大作中提到】 : : why can't? try this: : " : #include : class X : { : public: : X* getInstance() { return &_x; } : void setMember( int i ) { _i = i; } : int getMember() const { return _i; }
|
e****d 发帖数: 895 | 33 Try template class X instead of class X.
【在 h****e 的大作中提到】 : : why can't? try this: : " : #include : class X : { : public: : X* getInstance() { return &_x; } : void setMember( int i ) { _i = i; } : int getMember() const { return _i; }
|
h****e 发帖数: 2125 | 34
i though u said ctor and dtor should be private, right? i just didn't
put'em there.
【在 p*****d 的大作中提到】 : 用ctor定义pq,会有两份_i。
|
h****e 发帖数: 2125 | 35
why should I? of course if there's multiple template instantiations then
there will be multiple static members.
【在 e****d 的大作中提到】 : Try template class X instead of class X.
|
g*******e 发帖数: 3013 | 36 singleton就是singleton,咋还copy constructor。copy 出 double singleton? :)
getInstance不static没法call啊。:(
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
S****z 发帖数: 666 | 37 我想请教一下这个单件什么情况下会用到?
具体地有什么应用实例?
【在 g*******e 的大作中提到】 : singleton就是singleton,咋还copy constructor。copy 出 double singleton? :) : getInstance不static没法call啊。:(
|
p***o 发帖数: 1252 | 38 Whenever you attempt to use a global variable.
【在 S****z 的大作中提到】 : 我想请教一下这个单件什么情况下会用到? : 具体地有什么应用实例?
|
S****z 发帖数: 666 | 39 用全局变量就用全局变量啊,关单件什么事?
【在 p***o 的大作中提到】 : Whenever you attempt to use a global variable.
|
g*********s 发帖数: 1782 | 40 1. the default ctor is declared as private. but how about its copy ctor,
assignment, and dtor?
2. is the method of get_instance() required to be static? |
|
|
e****d 发帖数: 895 | 41 no public default constructor and public copy constructor.
get_instance() is static member function.
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
n**f 发帖数: 121 | 42 1. all private. if not sure, add assertions
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
e****d 发帖数: 895 | 43 copy assignment and destructor don't need to be private.
【在 n**f 的大作中提到】 : 1. all private. if not sure, add assertions
|
g*********s 发帖数: 1782 | 44
u haven't answered: is it required to be static? if so, why?
【在 e****d 的大作中提到】 : no public default constructor and public copy constructor. : get_instance() is static member function.
|
g*********s 发帖数: 1782 | 45 but is it preferred to be private or not?
【在 e****d 的大作中提到】 : copy assignment and destructor don't need to be private.
|
w***g 发帖数: 5958 | 46 不是static的你得有一个对象才能调用吧. single你在调用get_instance之前又不可能
有这个类的对象, 就只好static了.
【在 g*********s 的大作中提到】 : but is it preferred to be private or not?
|
p*****d 发帖数: 80 | 47 singleton
1. ctor: 必须private
否则会允许出现多个instance,这个和singleton的定义矛盾。而且必须定义无参数的ctor,否则编译器默认会定义一个。
2. get_instance method:必须static
否则无法在没有singleton instance的情况下调用产生第一个也是唯一一个instance。
3. copy ctor:必须private
否则下面的语句会出现两个instance
S *s = S::get_instance();
S s1(*s);
4. assignment optr:不必private
因为不会出现第二个instance,所以编译器默认生成的assignment operator只能自己assign自己,无效果。
5. dtor:看情况
dtor可能是有用的,如果允许用户自己释放singleton的话,因为singleton的定义
是无论何时在系统中最多有一个instance存在,可以是1个也可以是0个。但通常是定义一个release函数来free分配的内存而不是使用dtor。但即使dtor是public的,系统也不会去调用dtor,所以始终在程序不需要singleton的时候用release进行清理是好的习惯。
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
w***g 发帖数: 5958 | 48 赞牛人. 做学问做到这个地步就比较值得尊敬了.
ctor,否则编译器默认会定义一个。
【在 p*****d 的大作中提到】 : singleton : 1. ctor: 必须private : 否则会允许出现多个instance,这个和singleton的定义矛盾。而且必须定义无参数的ctor,否则编译器默认会定义一个。 : 2. get_instance method:必须static : 否则无法在没有singleton instance的情况下调用产生第一个也是唯一一个instance。 : 3. copy ctor:必须private : 否则下面的语句会出现两个instance : S *s = S::get_instance(); : S s1(*s); : 4. assignment optr:不必private
|
g*********s 发帖数: 1782 | 49 how about:
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
的ctor,
否则编译器默认会定义一个。
【在 p*****d 的大作中提到】 : singleton : 1. ctor: 必须private : 否则会允许出现多个instance,这个和singleton的定义矛盾。而且必须定义无参数的ctor,否则编译器默认会定义一个。 : 2. get_instance method:必须static : 否则无法在没有singleton instance的情况下调用产生第一个也是唯一一个instance。 : 3. copy ctor:必须private : 否则下面的语句会出现两个instance : S *s = S::get_instance(); : S s1(*s); : 4. assignment optr:不必private
|
w***g 发帖数: 5958 | 50 你这个自己做系统可以, 去面试肯定通不过的(我没学过DP, 曾经在面试时给出过你这
个答案. 这个太显然了,面试官不会满意的). 你要按playcod的答案去面试, 肯定无
往而不利.
【在 g*********s 的大作中提到】 : how about: : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : { : return &_x;
|
|
|
g*********s 发帖数: 1782 | 51 unfortunately, this is the example the interviewer showing to me.
he asks if get_instance() is required to be static and if it's required to
declare the singleton as a pointer instead of object.
【在 w***g 的大作中提到】 : 你这个自己做系统可以, 去面试肯定通不过的(我没学过DP, 曾经在面试时给出过你这 : 个答案. 这个太显然了,面试官不会满意的). 你要按playcod的答案去面试, 肯定无 : 往而不利.
|
e****d 发帖数: 895 | 52 how do you call get_X() ?
【在 g*********s 的大作中提到】 : how about: : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : { : return &_x;
|
g*********s 发帖数: 1782 | 53
yes, that's the point i missed.
【在 e****d 的大作中提到】 : how do you call get_X() ?
|
e****d 发帖数: 895 | 54 so, you know you cannot call a non-static member function
without an object?
【在 g*********s 的大作中提到】 : : yes, that's the point i missed.
|
p*****d 发帖数: 80 | 55 如果你的代码里的get_X改成static,在理论上是可以的。
我说的那五条里面只有最后一条需要改:dtor必须是private的,而且不能有release函
数,因为在singleton中可能会有其他的member是class,如果dtor被调用会出现double
free的问题,也就是说一些资源在s->~S()时被free了一次,最后调用s_instance的
dtor时又会被free一次。
但是,这不是好的做法,因为你失去了对资源何时建立以及释放的控制权。static S s
_instance在程序开始前被调用的问题有很多:比如初始化其中资源可能依赖于其他的
数据,而程序开始前这些数据尚未准备好,所以初始化这些资源是没有意义的;另外一
个问题是static变量的初始化顺序并不统一,理论上讲是header file被include的顺序
,但各个编译器实现并不一样,因此如果多个static的instance初始化不独立的话,这
会带来undetermined behavior,所以要尽可能避免class的静态instance。
【在 g*********s 的大作中提到】 : how about: : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : { : return &_x;
|
e****d 发帖数: 895 | 56 Calling dtor twice has nothing to do with singleton.
The same thing applies to calling dtor explicitly for
block scope objects.
double
s
【在 p*****d 的大作中提到】 : 如果你的代码里的get_X改成static,在理论上是可以的。 : 我说的那五条里面只有最后一条需要改:dtor必须是private的,而且不能有release函 : 数,因为在singleton中可能会有其他的member是class,如果dtor被调用会出现double : free的问题,也就是说一些资源在s->~S()时被free了一次,最后调用s_instance的 : dtor时又会被free一次。 : 但是,这不是好的做法,因为你失去了对资源何时建立以及释放的控制权。static S s : _instance在程序开始前被调用的问题有很多:比如初始化其中资源可能依赖于其他的 : 数据,而程序开始前这些数据尚未准备好,所以初始化这些资源是没有意义的;另外一 : 个问题是static变量的初始化顺序并不统一,理论上讲是header file被include的顺序 : ,但各个编译器实现并不一样,因此如果多个static的instance初始化不独立的话,这
|
p*****d 发帖数: 80 | 57 是和singleton的定义没关系,所以我说的是编程习惯的问题,要考虑到用户可能误用
,应该在compile time杜绝这种可能,但如果s_instance是pointer则dtor/release是
feature而非bug,这就是区别所在。
【在 e****d 的大作中提到】 : Calling dtor twice has nothing to do with singleton. : The same thing applies to calling dtor explicitly for : block scope objects. : : double : s
|
C**m 发帖数: 126 | 58 #include
#include
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
int main(int argc, char* argv[])
{
X *p;
std::cout << "the address " << (void *) p->get_X() << std::endl;
X a;
std::cout << "the address " << (void *) a.get_X() << std::endl;;
X b;
std::cout << "the address " << (void *) b.get_X() << std::endl;;
return 0;
}
/* the output is
* gcc 4.4.5
the address 0x804a180
the address 0x804a180
the address 0x804a180
*/ |
l*********s 发帖数: 5409 | 59 but that is not much different. You instantiate multiple wrapper objects and
all of them hold the unique static true object. right?
【在 e****d 的大作中提到】 : so, you know you cannot call a non-static member function : without an object?
|
t****t 发帖数: 6806 | 60 why do you want "wrapper objects" in the first place?
and
【在 l*********s 的大作中提到】 : but that is not much different. You instantiate multiple wrapper objects and : all of them hold the unique static true object. right?
|
|
|
l*********s 发帖数: 5409 | 61 jut wonder if this is an answer for the interview question.
【在 t****t 的大作中提到】 : why do you want "wrapper objects" in the first place? : : and
|
e****d 发帖数: 895 | 62 not sure how your p->get_X() can go through. p is not initialized
and applying -> operator on it should cause undefined behavor.
【在 C**m 的大作中提到】 : #include : #include : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : {
|
d****p 发帖数: 685 | 63 This should be fine as long as the this pointer is not used, which is the
case in his code.
【在 e****d 的大作中提到】 : not sure how your p->get_X() can go through. p is not initialized : and applying -> operator on it should cause undefined behavor.
|
t****t 发帖数: 6806 | 64 it's undefined, although in most cases you can run it.
【在 d****p 的大作中提到】 : This should be fine as long as the this pointer is not used, which is the : case in his code.
|
e****d 发帖数: 895 | 65 Since get_X() is a non-static member function, p is passed as "this" into
get_X(). How do you define whether a pointer is used or not in this case? |
d****p 发帖数: 685 | 66
In case p is uninitialized, what's wrong with
get_X(p) when get_X doesn't reference p? get_X clearly not a virtual
function. In this case get_X is actually a static member function which
takes in a redundant parameter.
Previously static member function was implemented as ((*)0)->mem
_func.
【在 e****d 的大作中提到】 : Since get_X() is a non-static member function, p is passed as "this" into : get_X(). How do you define whether a pointer is used or not in this case?
|
e****d 发帖数: 895 | 67 you are expecting the compiler does exactly what you intend to do.
However, the c++ spec says dereferecning a null/uninitialized
pointer has undefined behavior. Therefore, even your code works
in one compiler, it doesn't mean to work in other compilers.
mem
【在 d****p 的大作中提到】 : : In case p is uninitialized, what's wrong with : get_X(p) when get_X doesn't reference p? get_X clearly not a virtual : function. In this case get_X is actually a static member function which : takes in a redundant parameter. : Previously static member function was implemented as ((*)0)->mem : _func.
|
d****p 发帖数: 685 | 68
This is true.
However, in the case we discussed, NO compiler will ever deference the this
pointer.
【在 e****d 的大作中提到】 : you are expecting the compiler does exactly what you intend to do. : However, the c++ spec says dereferecning a null/uninitialized : pointer has undefined behavior. Therefore, even your code works : in one compiler, it doesn't mean to work in other compilers. : : mem
|
p*****d 发帖数: 80 | 69 这个例子太特殊了,没代表性,通常singleton中会有其他的数据,你给X加个其他
member就不行了。
【在 C**m 的大作中提到】 : #include : #include : class X { : public: X* get_X(); : private: : static X _x; : }; : X X::_x; : X* X::get_X() : {
|
h****e 发帖数: 2125 | 70
why can't? try this:
"
#include
class X
{
public:
X* getInstance() { return &_x; }
void setMember( int i ) { _i = i; }
int getMember() const { return _i; }
const int* getMemberAddr() const { return (long long)&_i; }
private:
X() {}
~X() {}
X( const X& );
X& operator=( const X& );
static X _x;
int _i;
};
X X::_x;
int main()
{
X* p;
std::cout << p->getInstance() << std::endl;
p->getInstance()->setMember( 10 );
std::cout << p->getInstance()->getMember() << std::endl;
std::cout << p->getInstance()->getMemberAddr() << std::endl;
X* q;
std::cout << q->getInstance()->getMember() << std::endl;
std::cout << q->getInstance()->getMemberAddr() << std::endl;
}
"
【在 p*****d 的大作中提到】 : 这个例子太特殊了,没代表性,通常singleton中会有其他的数据,你给X加个其他 : member就不行了。
|
|
|
p*****d 发帖数: 80 | 71 用ctor定义pq,会有两份_i。
【在 h****e 的大作中提到】 : : why can't? try this: : " : #include : class X : { : public: : X* getInstance() { return &_x; } : void setMember( int i ) { _i = i; } : int getMember() const { return _i; }
|
e****d 发帖数: 895 | 72 Try template class X instead of class X.
【在 h****e 的大作中提到】 : : why can't? try this: : " : #include : class X : { : public: : X* getInstance() { return &_x; } : void setMember( int i ) { _i = i; } : int getMember() const { return _i; }
|
h****e 发帖数: 2125 | 73
i though u said ctor and dtor should be private, right? i just didn't
put'em there.
【在 p*****d 的大作中提到】 : 用ctor定义pq,会有两份_i。
|
h****e 发帖数: 2125 | 74
why should I? of course if there's multiple template instantiations then
there will be multiple static members.
【在 e****d 的大作中提到】 : Try template class X instead of class X.
|
g*******e 发帖数: 3013 | 75 singleton就是singleton,咋还copy constructor。copy 出 double singleton? :)
getInstance不static没法call啊。:(
【在 g*********s 的大作中提到】 : 1. the default ctor is declared as private. but how about its copy ctor, : assignment, and dtor? : 2. is the method of get_instance() required to be static?
|
S****z 发帖数: 666 | 76 我想请教一下这个单件什么情况下会用到?
具体地有什么应用实例?
【在 g*******e 的大作中提到】 : singleton就是singleton,咋还copy constructor。copy 出 double singleton? :) : getInstance不static没法call啊。:(
|
p***o 发帖数: 1252 | 77 Whenever you attempt to use a global variable.
【在 S****z 的大作中提到】 : 我想请教一下这个单件什么情况下会用到? : 具体地有什么应用实例?
|
S****z 发帖数: 666 | 78 用全局变量就用全局变量啊,关单件什么事?
【在 p***o 的大作中提到】 : Whenever you attempt to use a global variable.
|
r****t 发帖数: 10904 | 79 However, operator-> is a non-static method, it is surprising that compilers
would directly go ahead to access other member without making sure an object
exists?
this
【在 d****p 的大作中提到】 : : This is true. : However, in the case we discussed, NO compiler will ever deference the this : pointer.
|
P********e 发帖数: 2610 | 80 member variable和function不存在同一个地方。access variable必须要object。
compilers
object
【在 r****t 的大作中提到】 : However, operator-> is a non-static method, it is surprising that compilers : would directly go ahead to access other member without making sure an object : exists? : : this
|