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. |