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 8848145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:21:33+00:00 2026-06-14T12:21:33+00:00

I am good at C++, but trying to understnd how singleton works. So I

  • 0

I am good at C++, but trying to understnd how singleton works. So I was looking into the code or article HERE .

If I will see the code;

class Singleton
{
private:
    static bool instanceFlag;
    static Singleton *single;
    Singleton()
    {/*private constructor*/}
public:
    static Singleton* getInstance();
    void method();
    ~Singleton()
    {instanceFlag = false;}
};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
    if(! instanceFlag)
    {
        single = new Singleton();
        instanceFlag = true;
        return single;
    }
    else
    {return single;}
}

void Singleton::method()
{cout << "Method of the singleton class" << endl;}

int main()
{
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc1->method();
    sc2 = Singleton::getInstance();
    sc2->method();

    return 0;
}

In this above code that prints Method of the singleton class twice.
If we wanted to print the output twice, then why do we need singleton.
We can write like;

sc1->method();
sc2->method();

Why do need such a complicated code as written above.
One thing I noticed, instanceFlag is becoming true once condition satisfies with onject sc1 but when the object sc2 gets called then it goes to else part.

So, what exactly we are trying to do here?
  • 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-14T12:21:34+00:00Added an answer on June 14, 2026 at 12:21 pm

    You might want to read up wikipedia to understand what Singleton means

    Essentially, for situations where you want to have only 1 object of a class at any time, you use such a pattern

    In this code, Singleton::getInstance() is meant to return this 1 object of the class. The first time you call it, the object is constructed. Subsequent calls to Singleton::getInstance() will be returned the same object. instanceFlag tracks if the object has been instantiated.

    Another hint is that the constructor is private, this means there is NO way to get an instance of the class except by the GetInstance() function.

    p.s. I do agree with you though, this code is more convoluted than im used to.. It also (technically) has a memory leak .. the instance is never deleted

    This is what i see more often

    static Singleton* getInstance()  
    {  
        static Singleton instance;  
        return &instance;  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to used following code but I am not getting good performance
I'm really confused here. I am trying to understand this code (it's javascript) but
I am trying to optimize this query as good as possible,but still i am
I'm trying to find a good python GUI framework for Mac OS X, but
Been trying to figure this out, but can't come up with a good solution
I am trying to play with php, I aint very good. But I figured
I am trying to customize a UITableView. So far, it looks good. But when
ok, i'm trying to do a quiz...all good by now. but when i'm trying
I have own component which works in my testing winform app good but when
I'm trying to understand how shifting with SSE works, but I don't understand the

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.