Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Singleton class in C++

Reply
Thread Tools

Singleton class in C++

 
 
bob smith
Guest
Posts: n/a
 
      08-20-2013
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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-20-2013
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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Roberto Waltman
Guest
Posts: n/a
 
      08-20-2013
bob smith wrote:

>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
--
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      08-20-2013
Paavo Helde wrote:
> 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.

 
Reply With Quote
 
Norman J. Goldstein
Guest
Posts: n/a
 
      08-20-2013
On 08/20/2013 01:19 PM, Luca Risolia wrote:
> Paavo Helde wrote:
>> 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.
>

Andrei Alexandrescu's "Modern C++ Design" has Chapter 6 on Singletons,
with explicit code. Covers design issues and approaches.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments