C++
  Home arrow C++ arrow Page 2 - C++ In Theory: The Singleton Pattern, Part...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

C++ In Theory: The Singleton Pattern, Part I
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 62
    2005-01-18

    Table of Contents:
  • C++ In Theory: The Singleton Pattern, Part I
  • A Logging Class
  • Statics are not Singletons
  • The Gamma Singleton
  • The Meyers Singleton

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C++ In Theory: The Singleton Pattern, Part I - A Logging Class


    (Page 2 of 5 )

    Here is a simple class that collects strings and can write them to a file. This class will represent the log we aim to implement in this article.

    // log.h
    #ifndef __LOG_H
    #define __LOG_H

    #include <list>
    #include <string>

    class Log {
    public:
      void Write(char const *logline);
      bool SaveTo(char const *filename);
    private:
      std::list<std::string> m_data;
    };
    #endif // __LOG_H
    // eof

    // log.cpp
    #include <fstream>
    using namespace std;

    #include “log.h”

    void Log::Write(char const *logline)
    {
    m_data.push_back(logline);
    }

    bool Log::SaveTo(char const *filename)
    {
      ofstream file(filename, ios::out);
      list<string>::iterator it;
      list<string>::iterator lastIt=m_data.end();
      for (it=m_data.begin(); it!=lastIt; ++it)
        file << *it << endl;
      file.close();
    }

    // eof

    We are going to alter the behavior of this class to ensure that only one instantiation of it can be created during the lifetime of an application using it. This will cause all strings provided in calls to Log::Write, to end up in a single list (Log::m_data) which can be written to a single file when Log::Write is called.

    More C++ Articles
    More By J. Nakamura


       · The article isw very good for a beginner showing him how the solution...
       · class LogNetwork : public LogNetwork {};I don't think this is right.a class...
       · You are very right it should beclass LogNetwork : public LogBasethx for...
       · "...which can be written to a single file when Log::Write is called."That should...
       · You discussed using polymorphism to have different logging behaviors, and using the...
     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT