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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:40:06+00:00 2026-05-25T10:40:06+00:00

When I look for informations about the singleton pattern for C++, I always find

  • 0

When I look for informations about the singleton pattern for C++, I always find examples like this:

class Singleton
{
    public:
        ~Singleton() {
        }

        static Singleton* getInstance()
        {
            if(instance == NULL) {
                instance = new Singleton();
            }
            return instance;
        }

    protected:
        Singleton() {
        }

    private:
        static Singleton* instance;
};

Singleton* Singleton::instance = NULL;

But this kind of singleton also seems to work as well:

class Singleton
{
    public:
        ~Singleton() {
        }

        static Singleton* getInstance()
        {
            return &instance;
        }

    protected:
        Singleton() {
        }

    private:
        static Singleton instance;
};

Singleton Singleton::instance;

I guess that the second singleton is instantiated at the beginning of the program, unlike the first, but is it the only difference?

Why do we find mainly the first?

  • 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-05-25T10:40:07+00:00Added an answer on May 25, 2026 at 10:40 am

    http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14

    The static initialization order fiasco is a very subtle and commonly
    misunderstood aspect of C++. Unfortunately it’s very hard to detect —
    the errors often occur before main() begins.

    In short, suppose you have two static objects x and y which exist in
    separate source files, say x.cpp and y.cpp. Suppose further that the
    initialization for the y object (typically the y object’s constructor)
    calls some method on the x object.

    That’s it. It’s that simple.

    The tragedy is that you have a 50%-50% chance of dying. If the
    compilation unit for x.cpp happens to get initialized first, all is
    well. But if the compilation unit for y.cpp get initialized first,
    then y’s initialization will get run before x’s initialization, and
    you’re toast. E.g., y’s constructor could call a method on the x
    object, yet the x object hasn’t yet been constructed.

    The first method you listed avoids this problem completely. It’s called the “construct on first use idiom”

    The downside of this approach is that the object is never destructed.
    There is another technique that answers this concern, but it needs to
    be used with care since it creates the possibility of another (equally
    nasty) problem.

    Note: The static initialization order fiasco can also, in some cases,
    apply to built-in/intrinsic types.

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

Sidebar

Related Questions

All examples of svn branching I have seen so far look like this svn
Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
Delegates look like such a powerful language feature, but I've yet to find an
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
Look at this situation: www.websitea.com displays an img tag with a src attribute of
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
Look please below this codes throw me : FormatException was unhandled by user code
I recently started learning about Chrome Extensions and was taking a look at the
I'd like to be able to screenscrape Morningstar webpages. Morningstar provides information about a

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.