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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:55:44+00:00 2026-06-17T02:55:44+00:00

I try to write a singleton class so that a class only needs to

  • 0

I try to write a singleton class so that a class only needs to derive from it and be automatically singleton:

Base class:

 template <class T>
 class Singleton
 {
 private:
     static T* instance;
 public:

     static T* getInstance(void)
     {
         if(!instance)
             instance = new T;
         return instance;
     }

     static void release(void)
     {
         if(instance)
         {
             delete instance;
             instance = NULL;
         }
     }
 };

 template <class T>
 T* Singleton<T>::instance = NULL;

My derived class:

#include <iostream>
#include "Singleton.h"

class MySingleton : public Singleton<MySingleton>
{
public:
    void helloWorld(void)
    {
        printf("Hello, World!\n");
    }
};

main:

int main(int argc, const char * argv[])
{
    MySingleton* s = MySingleton::getInstance();
    s->helloWorld();
    return 0;
}

This works perfectly, but it’s not a real singleton, since i can still construct MySingleton using it’s default constructor. I can, of course, make MySingleton’s ctors private and declare Singleton a friend, but is there any way i can do that in the base class, so that just deriving and not declaring any ctors is enough to make a class singleton?

  • 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-17T02:55:45+00:00Added an answer on June 17, 2026 at 2:55 am

    Since your base class template has to construct the singleton object, it needs access to the concrete class’ constructor. Therefore, that constructor should be either public or the base class has to be a friend of the concrete class. A public Ctor in the concrete class is accessible by everyone, so you can not prohibit its use at compile time.
    You can, however, assure, that it is only called once, at run time:

    template <class T>
    class Singleton
    {
      /* ... */
      static bool instantiating;
    protected:
      Singleton()
      {
        if (!instantiating) //not called in instance() function
          throw std::runtime_error("Don't call me!");
      }
    
    public:
      static T* getInstance()
      {
        intantiating = true;
        instace = new T();
        instantiating = false;
      }
    };
    
    template <class T>
    bool Singleton<T>::instantiating = false;
    

    Notes:
    – The instantiating variable as I use it here is not threadsafe
    – The true/false setting of instantiating is not exceptionsafe (new or T::T() might throw)
    – Your use of a pointer as instance variable is insecure and prone to memleaks. Consider using shared_ptr or a reference (Meyers singleton)

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

Sidebar

Related Questions

I try to write DSL class Warcraft class << self def config unless @instance
I try to write a class that takes a tuple of functions as its
@Singleton public class EntityManagerFactoryUtil implements Serializable { private static final long serialVersionUID = -5769561190804055727L;
try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that
I try to write a macro in clojure that sets up a namespace and
I created a Singleton class in c#, with a public property that I want
class ExtHotelApiService extends HotelApiService { static scope = singleton static transactional = true def
class LogWriter implements Runnable { Socket client; private static ThreadLocal<Date> date = new ThreadLocal<Date>()
I'm trying to make a class that takes info from the GUI this saves

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.