Re: singleton
[top-posting corrected]
Colin:
class A{
private:
A(){}
static A* a=null;
public:
static A *getInstance(){
if(a==null)
{
a=new A(); //remeber delete it..
}
return a;
}
};
Thomas:
hi guys,
-----code-----
class A{
private:
A(){}
public:
static A *getInstance(){
static A *a = new A(); //-------L1----
return a;
}};
ManicQin wrote:
> Thmas if you look closly you'll see the difference between what you
> wrote and what Colin wrote
> Colin's class A has a static member variable that the singleton is
> responsible for keeping only one instance of him in the system.
> Thomas's code is a singleton that creates new instances of a local
> variable each time getInsatnce is called, (which is not very singleton
> of him... Unless Thomas gave a wrong code...)
That's wrong. Both versions create only one instance of the object.
A variable declared 'static' locally in a function body is initialized only
once.
--
Thomas
http://www.netmeister.org/news/learn2quote.html
"Some folks are wise, and some otherwise."