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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:55+00:00 2026-05-13T11:04:55+00:00

how to answer this question?

  • 0

how to answer this question?

  • 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-13T11:04:55+00:00Added an answer on May 13, 2026 at 11:04 am

    EDIT:

    Oops, the answer no. As others have pointed out, simply setting all methods/members to static follows the Monostate pattern (of which I was not aware). I was too eager to show off my shiny Singleton template (a simplified version of Alexandrescu’s SingletonHolder, really).

    This answer should be downvoted.

    Original Answer:

    Yes. But it is less flexible than other ways of designing singletons. See Modern C++ Design by Alexandrescu and his Loki library: http://en.wikipedia.org/wiki/Loki_%28C%2B%2B%29

    If you have several static singletons that depend on each other and on other global objects, you risk having problems because the order of their initialization (before main() kicks in) is tricky and can lead to unexpected results.

    Using templates, you can convert normal classes into singletons. If you later decide that your singleton is no longer a singleton (i.e. you can have multiple instances), then you don’t have to convert all the class’s methods to non-static.

    One way, using templates, is something like this:

    template <class T>
    class Singleton
    {
    public:
       static T& instance()
       {
          static T singleton;
          return singleton;
       }
    
    private:
       Singleton() {} // Disallow construction of Singleton<T> instances
    }
    
    class Foo
    {
    public:
       void print() {std::cout << "Hello world\n";}
    };
    
    typedef Singleton<Foo> TheFoo;
    
    main()
    {
       TheFoo::instance().print();
    }
    

    Note that this does not prevent you from creating Foo instances, unless you make the Foo constructor private (and make Singleton a friend of Foo).

    An advantage with this method over all-static classes is that you have more control over when the singleton object is constructed. It’ll be constructed the first time you access the singleton. So you can have something like this:

    main()
    {
       TheFoo::instance(); // Make sure the Foo is constructed before the Bar
       TheBar::instance();
    }
    

    There is debate over of the appropriateness of Singletons. Some say they are global objects in disguise and can make your code less reusable. I will not comment further on that, as I have not made up my mind myself.

    EDIT:

    If you find TheFoo::instance() too verbose, you can always provide an inline shortcut function or use references:

    inline Foo& theFoo() {return TheFoo::instance();}
    
    main()
    {
       theFoo().makeMeSomeCoffee("1 milk, 1 sugar");
       Foo& foo = theFoo();
       foo.makeMeASandwich("BLT");
    }
    

    Mmmm…. off to make coffee & sandwich.

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

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer With this helper I believe you need to pass all… May 15, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer After reading the text from DataSet, used the following encoding… May 15, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer You can use a ToggleButton instead of the normal Button… May 15, 2026 at 3:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.