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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:30:00+00:00 2026-06-12T12:30:00+00:00

I would like to have a class T that can generate only 1 instance

  • 0

I would like to have a class T that can generate only 1 instance in the whole program.

Now i know about std::unique_ptr but there are 2 problems:

  • it’s limited to a scope ( but it’s not a big issue … )
  • it needs to be explicitly used, meaning that it’s not part of the class or the type, it’s just and handler and a special pointer, but it does not modify the design of my class.

now i would like to have class T designed in a way that not even by mistake the user can declare 2 instances in the same program and i can’t rely on the fact that my user will declare an std::unique_ptr for T because i want to solve this by design.

right now i’m only thinking about how to make an implicit use of an unique_ptr in an elegant way, the problem is that i do not have any clue at the moment.

the other way around is to check if this class is handled by an unique_ptr but this check will make me lose an edge in terms of performances.

since having only 1 instance is really important, i see only 2 options in my case: 1) trying to solve this by design 2) throwing errors at compile time with some sort of check/macro.

I know that this looks trivial but with a design approach it’s not, at least for me, so please 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-06-12T12:30:02+00:00Added an answer on June 12, 2026 at 12:30 pm

    What you’re looking for is called the Singleton pattern, and while it is widely considered by many (myself included) to be an anti-pattern, I will nonetheless show you the basic elements needed to build one.

    Basically what you need to do is provide three things:

    1. A static method which “gets” the one and only instance
    2. A private constructor, so that nobody can ever instantiate it
    3. (optional) A means by which the one and only instance is created before main starts

    Here’s the essential code:

    class Singleton
    {
    public:
      Singleton&  get() 
      { 
        static Singleton me_;
        return me_;
      }
    private:
      Singleton() {};
    };
    

    I leave it to you to discover how to implement #3 above, and why you shouldn’t be using a Singleton in the first place — there are many reasons.

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

Sidebar

Related Questions

I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I have a class that is marked with DataContract attributes and I would like
I have a class that is called regularly by several objects. I would like
I have a class that contains decoded video frames. I would like my decoder
I have a class in silverlight that I would like to store to disk.
I have a custom-made view that extends the View class. I would like 2
I would like to have a class B which can be extended with two
What I wanna do is a method that can generate instance of Class X
I have designed some classes using Visual Studio class diagramming. Now I would like
I have a Ruby script that defines a class. I would like the script

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.