Singleton class in C++

B

bob smith

If someone asks you to write a Singleton class in C++, can someone help me see what the answer would be?

In particular, I don't know if the getInstance() function ought to return Singleton* or Singleton&.

I'm also concerned about when and how the memory for this class will be deallocated.

Thanks.
 
I

Ian Collins

bob smith wrote:

{please clean up the mess google will inevitably make of your replies!}
If someone asks you to write a Singleton class in C++, can someone
help me see what the answer would be?

That depends on the exact requirements. Something as simple as

template <typename T>
struct Singleton
{
static T& getInstance()
{
static T instance;

return instance;
}
};

is often good enough.
In particular, I don't know if the getInstance() function ought to
return Singleton* or Singleton&.

As above.
I'm also concerned about when and how the memory for this class will
be deallocated.

In the example I cited, the instance will be constructed the first time
getInstance() is called and destroyed when the programme exits.
 
R

Roberto Waltman

bob said:
If someone asks you to write a Singleton class in C++ ...

An alternative to Ian Collins approach, that I find useful, is a
"Monostate" class. (1)
That is, a class were all data members are static, so every instance
shares a common state.
In particular, I don't know if the getInstance() function ...

Not getInstance() method is required. Instances of such classes can be
declared at will without allocating additional storage (beyond minimal
size and alignment requirements + the rule that every object should
have a unique address.)
I'm also concerned about when and how the memory for this class will be deallocated.
As in Ian's example, memory will be released when the program exits.

(1) From Stephen Dewhurst's book "C++ Common Knowledge"
See also http://c2.com/cgi/wiki?MonostatePattern
 
L

Luca Risolia

Paavo said:
Sometimes it
is simpler to allocate singleton objects dynamically and never destroy
them. This leaves the memory cleanup to the OS on the process shutdown.
However, this practice may be formally considered as a memory leak (for
some useless definition of "memory leak") and thus frowned upon by some
people.

In those cases, the instance can be wrapped with a static unique_ptr to avoid
resource leaking.
 
N

Norman J. Goldstein

In those cases, the instance can be wrapped with a static unique_ptr to avoid
resource leaking.
Andrei Alexandrescu's "Modern C++ Design" has Chapter 6 on Singletons,
with explicit code. Covers design issues and approaches.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top