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

I know I should do my homework on my own but I simply can't
I need some advice here. I need to make a webservice, that can make
I have some code that grabs some rows from a table and I want
Is it possible to make a C++ header file (.h) that declares a class,
I have a simple question. Should I return a byte-array or simply base64 encode
It should be simple: Hook a MODEL_save_after event (or MODEL_save_before if that’s more appropriate).
This should seem simple enough, but can't figure it out. I was porting a
I am making a site that consumes a csv file this file can come
I want to a rewrite rule such that if a user goes to the
I have a class Employee. I want to be able to Validate() it before

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.