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.
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 { public: static Log& Instance() { static Log theLog; return theLog; } void Write(char const *logline); bool SaveTo(char const *filename); private: 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:
In the next article in this series, we will look at how to generalize this class, and at the construction/destruction sequencing problems.
REFERENCES
[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.