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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:15:41+00:00 2026-05-27T19:15:41+00:00

Possible Duplicate: A Singleton that is not globally accessible Do you know a good

  • 0

Possible Duplicate:
A Singleton that is not globally accessible

Do you know a good design pattern that would make sure that only one instance of an object is created without making this object global in c++?
That’s what singleton do but I really need it NOT to be global for code access security reasons.

Thanks for your help!

  • 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-27T19:15:41+00:00Added an answer on May 27, 2026 at 7:15 pm

    I think you want something like this (note: copy-pasted from an answer I already wrote and forgot about):

    #include <stdexcept>
    
    // inherit from this class (privately) to ensure only
    // a single instance of the derived class is created
    template <typename D> // CRTP (to give each instantiation its own flag)
    class single_instance
    {
    protected: // protected constructors to ensure this is used as a mixin
        single_instance()
        {
            if (mConstructed)
                throw std::runtime_error("already created");
    
            mConstructed = true;
        }
    
        ~single_instance()
        {
            mConstructed = false;
        }
    
    private:
        // private and not defined in order to
        // force the derived class be noncopyable
        single_instance(const single_instance&);
        single_instance& operator=(const single_instance&);
    
        static bool mConstructed;
    };
    
    template <typename T>
    bool single_instance<T>::mConstructed = false;
    

    Now you get an exception if the class is constructed more than once:

    class my_class : private single_instance<my_class>
    {
    public:
        // usual interface (nonycopyable)
    };
    
    int main()
    {
        my_class a; // okay
        my_class b; // exception
    }
    

    There’s no way to enforce the single-instance policy at compile-time in C++, though.


    (Also good on you for noticing that singletons are dumb. Globally accessible and singly-creatable are two different concepts, and should only be combined by coincidence, not by design.)

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

Sidebar

Related Questions

Possible Duplicate: C++ Singleton design pattern. How can I create only one instance of
Possible Duplicate: How are singletons handled in a web application? Is singleton design pattern
Possible Duplicate: What is so bad about Singletons? It's understandable that many design patterns
Possible Duplicate: Efficient way to implement singleton pattern in Java I was reading this
Possible Duplicate: Difference between static class and singleton pattern? we use static class for
Possible Duplicate: Difference between static class and singleton pattern? I'm thinking about the choice
Possible Duplicate: Difference between static class and singleton pattern? Which is better in Java,
Possible Duplicate: What's the purpose of the Tuple(T1)/Singleton in .net? Is the only reason
Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: What's Alternative to Singleton If I am not allowed to use 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.