By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

how to replace function-scoped static singleton

P: n/a
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;
}
Nov 19 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 19 '08 #2

P: n/a
Bob Doe <Du*********@gmail.comkirjutas:
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

Nov 19 '08 #3

P: n/a
On Nov 19, 10:25 pm, Paavo Helde <pa...@nospam.please.orgwrote:
Bob Doe <DumpForJ...@gmail.comkirjutas:
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
Nov 20 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.