Connecting Tech Pros Worldwide Forums | Help | Site Map

how to replace function-scoped static singleton

Bob Doe
Guest
 
Posts: n/a
#1: Nov 19 '08
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;
}



Victor Bazarov
Guest
 
Posts: n/a
#2: Nov 19 '08

re: how to replace function-scoped static singleton


Bob Doe wrote:
Quote:
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.
Quote:
>
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
Guest
 
Posts: n/a
#3: Nov 19 '08

re: how to replace function-scoped static singleton


Bob Doe <DumpForJunk@gmail.comkirjutas:
Quote:
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.
Quote:
...
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
Guest
 
Posts: n/a
#4: Nov 20 '08

re: how to replace function-scoped static singleton


On Nov 19, 10:25 pm, Paavo Helde <pa...@nospam.please.orgwrote:
Quote:
Bob Doe <DumpForJ...@gmail.comkirjutas:
>
Quote:
Hello,
>
Quote:
how to I replace singleton classes using function scope static
variables with one that doesn't use function scope static variables?:
>
Quote:
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.
>
Quote:
...
private:
Foo();
Foo(const Foo&);
Foo & operator=(const Foo&);
};
>
Quote:
----------------------------------
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
Closed Thread