James Kanze
11/20/2008 8:38:00 AM
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