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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:24:58+00:00 2026-06-07T04:24:58+00:00

Possible Duplicate: What is so bad about singletons? Singleton pattern in C++ I want

  • 0

Possible Duplicate:
What is so bad about singletons?
Singleton pattern in C++

I want to create a singleton class. For that, I created a class with all its member and methods as static. Something like this.

class a
{
    static int a;
    static GetA();
}

Now all the classes who want to use my singleton class cannot create any object for my class and also will get same value. I just want to know whether this implementation will solve all the purpose and fulfill all criteria for creating a singleton class.

  • 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-07T04:25:00+00:00Added an answer on June 7, 2026 at 4:25 am

    The conventional Singleton (anti-)pattern isn’t a collection of static variables; rather, it is an object with non-static members, of which only one instance can exist.

    In C++, this allows you to avoid the biggest problem with static variables: the “initialisation order fiasco”. Because the initialisation order is unspecified for static variables in different translation units, there’s a danger that the constructor of one might try to access another before it is initialised, giving undefined behaviour. However, it introduces other problems (a similar “destruction order fiasco”, and thread safety issues in older compilers), so it’s still something to avoid.

    If you want a collection of static variables and functions, then put them in a namespace rather than a class:

    namespace stuff {
        int a;
        void do_something();
    }
    

    If you think you want a singleton, then think again; you’re generally better avoiding globally accessible objects altogether. If you still want one, then you would make a class with a private constructor, and a public accessor that returns a reference to the single instance, along the lines of:

    class singleton {
    public:
        singleton & get() {
            static singleton instance;
            return instance;
        }
    
        int a;
        void do_something();
    
    private:
        singleton() {}
        ~singleton() {}
        singleton(singleton const &) = delete;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is so bad about Singletons? It's understandable that many design patterns
Possible Duplicates: What is so bad about Singletons Problems with Singleton Pattern Are there
Possible Duplicate: What is so bad about singletons? I've read in several answers to
Possible Duplicate: Initialize class fields in constructor or at declaration? We are arguing about
Possible Duplicate: PHP: Require() and Class Hierarchies Is this bad practice? Is there any
Possible Duplicate: Why is require_once so bad to use? I've read somewhere that the
Possible Duplicate: Choosing a Java Web Framework now? Hi All, I'm thinking about which
Possible Duplicate: R sorts a vector on its own accord - bad boy! How
Possible Duplicate: Are iframes considered 'bad practice'? Some say that iFrame is evil. But
Possible Duplicate: Button half width of screen I want to create two buttons inside

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.