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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:24:39+00:00 2026-06-15T14:24:39+00:00

Is it possible to make a generic singleton? I want to create something I

  • 0

Is it possible to make a generic singleton? I want to create something I can just inherit from and get the functionality of a singleton. I’m having trouble with using templates with static members.

Shouldn’t this work?

**

UPDATED

**

Thanks for the replies so far. So now my problem is that GameEngine can’t see it’s own constructor. I know it’s private but still.

Singleton.h

template <typename T>
class Singleton
{
private:
    static std::shared_ptr<T> m_Instance;
public: 
    static std::shared_ptr<T> Instance();
};

template<typename T>
std::shared_ptr<T> Singleton<T>::m_Instance = nullptr;

template<typename T>
std::shared_ptr<T> Singleton<T>::Instance()
{
if(m_Instance == nullptr)
    m_Instance = std::make_shared<T>();

return m_Instance;
}

GameEngine.h

class GameEngine : public Singleton<GameEngine>
{
private:
GameEngine();

};
  • 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-15T14:24:40+00:00Added an answer on June 15, 2026 at 2:24 pm

    The best way to create a singleton is as follows:

    class GameEngine
    {
    public:
        static GameEngine& instance() { static GameEngine e; return e; }
    
    private:
        GameEngine() {}
        GameEngine(const GameEngine&) = delete;
    };
    

    The e variable will be (thread-safely) initialized on first call to instance, and it will be destroyed on orderly process exit. The private/deleted constructors prevent a second instance from ever being created.

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

Sidebar

Related Questions

Is it possible to create a generic pager (ASCX UserControl) which can be used
Is it possible make some handler that will do something when user shutdown computer
Possible Duplicate: Make iPhone app paid version replace free version on install from app
Is it possible to make custom 404 pages for specific subdirectories instead of having
Is it possible to make a Func delegate an extension method? For example, just
I'm starting to consider creating a class library that I want to make generic
Is it possible to make a generic class that acts as a dynamic array
I am trying to make a generic URL rewrite methods, and i want it
I'm building an infrastructure for several websites. I want to make a generic/abstract infrastructure
Is is possible to make the following compile without: Making IFooCollection generic Explicitly implementing

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.