Home arrow C++ arrow Page 5 - C++ In Theory: The Singleton Pattern, Part I

C++ In Theory: The Singleton Pattern, Part I

Have you ever wondered how to implement a class with simple logging functionality? J. Nakamura explains how to do it in a way that makes use of the Singleton pattern.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 87
January 18, 2005
  1. · C++ In Theory: The Singleton Pattern, Part I
  2. · A Logging Class
  3. · Statics are not Singletons
  4. · The Gamma Singleton
  5. · The Meyers Singleton

print this article

C++ In Theory: The Singleton Pattern, Part I - The Meyers Singleton
(Page 5 of 5 )

Luckily there is another solution that is very easy to implement and we call it the Meyers Singleton [Meyers]:

class Log {
  static Log& Instance() {
    static Log theLog;
    return theLog;
  void Write(char const *logline);
  bool SaveTo(char const *filename);
  Log();          // ctor is hidden
  Log(Log const&);      // copy ctor is hidden
  Log& operator=(Log const&);  // assign op is hidden

  static std::list<std::string> m_data;

This construction relies on the fact that function-static objects are only initialized when the function is first being called upon; thus we maintain the benefit of dynamic initialization. (Function-static primitive variables like static int number=100; do get translated during compile time however!)

An additional safeguard is the returned reference instead of the pointer. The caller cannot be tempted to delete the singleton (as was the case when we were returning a pointer). The constructor is hidden, making it impossible for a user to instantiate a Log object; it would still be possible to assign the singleton Log to itself, however, and since this makes no sense we might just as well hide the assignment operator.

  Log::Instance().Write(“Test line”);  // works fine
  Log &myLog = Log::Instance();    // works fine
  Log another = myLog;  // FAILS
  myLog = myLog;    // works until we hide the assign ops!

To wrap it all up, here is an abstract of the Meyers Singleton:

class Singleton {
static Singleton& Instance() {
  static Singleton theSingleton;
  return theSingleton;
/* more (non-static) functions here */
Singleton(); // ctor hidden
Singleton(Singleton const&); // copy ctor hidden
Singleton& operator=(Singleton const&); // assign op. hidden
~Singleton(); // dtor hidden

In the next article in this series, we will look at how to generalize this class, and at the construction/destruction sequencing problems.


[Gamma] Design Patterns: E.Gamma, R.Helm, R.Johnson and J.Vlissides.
[Meyers] More Effective C++: S.Meyers.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials