Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:23:00+00:00 2026-06-08T12:23:00+00:00

Possible Duplicate: Can any one provide me a sample of Singleton in c++? C++

  • 0

Possible Duplicate:
Can any one provide me a sample of Singleton in c++?
C++ Singleton design pattern
C++ different singleton implementations

I need some example of Singleton in c++ class because i have never wrote such class.
For an example in java I can declare d a static field which is private and it’s initialize in the constructor and a method getInstance which is also static and return the already initialize field instance.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:23:01+00:00Added an answer on June 8, 2026 at 12:23 pm

    Example:
    logger.h:

    #include <string>
    
    class Logger{
    public:
       static Logger* Instance();
       bool openLogFile(std::string logFile);
       void writeToLogFile();
       bool closeLogFile();
    
    private:
       Logger(){};  // Private so that it can  not be called
       Logger(Logger const&){};             // copy constructor is private
       Logger& operator=(Logger const&){};  // assignment operator is private
       static Logger* m_pInstance;
    };
    

    logger.c:

    #include "logger.h"
    
    // Global static pointer used to ensure a single instance of the class.
    Logger* Logger::m_pInstance = NULL; 
    
    /** This function is called to create an instance of the class.
        Calling the constructor publicly is not allowed. The constructor
        is private and is only called by this Instance function.
    */
    
    Logger* Logger::Instance()
    {
       if (!m_pInstance)   // Only allow one instance of class to be generated.
          m_pInstance = new Logger;
    
       return m_pInstance;
    }
    
    bool Logger::openLogFile(std::string _logFile)
    {
        //Your code..
    }
    

    more info in:

    http://www.yolinux.com/TUTORIALS/C++Singleton.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How does Python compare string and int? Can any one explain the
Possible Duplicate: Can any one help me, to create .apk file for google android
Possible Duplicate: Regular expression that matches valid IPv6 addresses Can any one know the
Possible Duplicate: .net open source and running platform choices Can any one tell me
Possible Duplicate: What is a stack overflow error? Can any one tell me how
Possible Duplicate: Creating an abstract class in Objective C Hi Can any one please
Possible Duplicate: JDBC support on J2ME I am not getting it. Can any one
Possible Duplicate: What do two question marks together mean in C#? can any one
Possible Duplicate: VB.NET to C# - my.computer.getfiles() Can any one tell me the equivalent
Possible Duplicate: Source Control for a .Net guy Can any one help me to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.