[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

how to replace function-scoped static singleton

Bob Doe

11/19/2008 2:02:00 AM

Hello,

how to I replace singleton classes using function scope static
variables with one that doesn't use function scope static variables?:

class Foo {
public:
static Foo &instance();
virtual ~Foo();
...
private:
Foo();
Foo(const Foo&);
Foo & operator=(const Foo&);
};

----------------------------------
Foo &Foo::instance()
{
static Foo& theInstance;
...
return theInstance;
}


3 Answers

Victor Bazarov

11/19/2008 2:39:00 AM

0

Bob Doe wrote:
> how to I replace singleton classes using function scope static
> variables with one that doesn't use function scope static variables?:

What is the problem? What are you trying to replace it with?
Anything in particular? "Not using function scope" is not much
of a specification. Have you looked at possible implementations
of the Singleton pattern? Try googling it. Try looking in some
smart books (like the GoF one). Try looking in the archives.

This has been discussed so many times that it just doesn't need
to be repeated, honestly.

>
> class Foo {
> public:
> static Foo &instance();
> virtual ~Foo();
> ...
> private:
> Foo();
> Foo(const Foo&);
> Foo & operator=(const Foo&);
> };
>
> ----------------------------------
> Foo &Foo::instance()
> {
> static Foo& theInstance;
> ...
> return theInstance;
> }

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Paavo Helde

11/19/2008 9:26:00 PM

0

Bob Doe <DumpForJunk@gmail.com> kirjutas:

> Hello,
>
> how to I replace singleton classes using function scope static
> variables with one that doesn't use function scope static variables?:
>
> class Foo {
> public:
> static Foo &instance();
> virtual ~Foo();

Not much point to have public virtual dtor for a singleton object which
is never destroyed ;-) But it does not hurt, of course.

> ...
> private:
> Foo();
> Foo(const Foo&);
> Foo & operator=(const Foo&);
> };
>
> ----------------------------------
> Foo &Foo::instance()
> {
> static Foo& theInstance;

This won't compile.

Function scope static variables are a proven method for creating
singletons. If this does not work for you, you should provide some
explanation about your worries (e.g. multithreading concerns, memory leak
alarms, etc...)

Paavo



James Kanze

11/20/2008 8:38:00 AM

0

On Nov 19, 10:25 pm, Paavo Helde <pa...@nospam.please.org> wrote:
> Bob Doe <DumpForJ...@gmail.com> kirjutas:
>
> > Hello,
>
> > how to I replace singleton classes using function scope static
> > variables with one that doesn't use function scope static variables?:
>
> > class Foo {
> > public:
> > static Foo &instance();
> > virtual ~Foo();
>
> Not much point to have public virtual dtor for a singleton object which
> is never destroyed ;-) But it does not hurt, of course.
>
> > ...
> > private:
> > Foo();
> > Foo(const Foo&);
> > Foo & operator=(const Foo&);
> > };
>
> > ----------------------------------
> > Foo &Foo::instance()
> > {
> > static Foo& theInstance;
>
> This won't compile.
>
> Function scope static variables are a proven method for creating
> singletons. If this does not work for you, you should provide some
> explanation about your worries (e.g. multithreading concerns, memory leak
> alarms, etc...)
>
> Paavo