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

  • Home
  • SEARCH
  • 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 3224238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:11:59+00:00 2026-05-17T16:11:59+00:00

Ok, I should simply tell that I want to make a base Singleton class

  • 0

Ok, I should simply tell that I want to make a base Singleton class that I can inherit from, and the way I want to achieve that is by a template.

In order to avoid memory leaks, I do not use directly a pointer to the instance, but a private class that will handle deleting the pointer.

Here is my actual code (not working) :

template <typename T> class Singleton
{
private:
    class PointerInstance
    {
    private:
        T* instance;
    public:
        PointerInstance() : instance(0) {}
        ~PointerInstance() { delete instance; } // no memory leak !
        T* Get()
        {
            if ( !instance ) {
                instance = new T();
            }
            return instance;
        }
    };
    static PointerInstance PInstance;
public:
    static T* pGetInstance(void)
    {
        return PInstance.pGet();
    };
protected:
    Singleton(void){};
    ~Singleton(void){};
};

And here is what a typical derived class declaration should look like :

class Child : public Singleton<Child>
{
    friend class Singleton<Child>;
    Child();
    // etc...
};

Basically what is lacking is the instance of PInstance for each T class I make as a Singleton.

My question is : is there a way to do this once and for all with a few generic lines of code in the Singleton.h containing the code above, or do I have no other choice but to add a few specific lines of code for each derived class ?

(Bonus : is there a better way to do a Singleton class in C++ ?)

  • 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-17T16:11:59+00:00Added an answer on May 17, 2026 at 4:11 pm

    Here is a simpler way of writing a CRTP Singleton without a memory leak:

    template <class T>
    class Singleton
    {
      friend class T;
    private:
      Singleton() {};
      ~Singleton() {};
      Singleton(const Singleton&); // not implemented
      const Singleton& operator=(const Singleton&); // not implemented
    
    public:
      static T* pGetInstance()
      {
        static T theInstance;
        return &theInstance;
      }
    };
    

    Usage is the same as in the question.

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

Sidebar

Related Questions

My scenario should be simple... the type I want to convert FROM is ALWAYS
Is it semantically correct to nest definition lists, or should they simply be a
How often should you use git-gc? The manual page simply says: Users are encouraged
It is discouraged to simply catch System.Exception . Instead, only the "known" exceptions should
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
Should be simple and quick: I want a C# equivalent to the following Java
Should be pretty simple: I have an InputStream where I want to peek at
I know this should be simple and I should know it but it's eluding
This should be a simple question, but I haven't been able to find a
I'm attempting to do something which should be relatively simple. I'm using a ListView

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.