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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:41:23+00:00 2026-06-07T13:41:23+00:00

I have an abstract Singleton class. My goal is that any subclasses just have

  • 0

I have an abstract Singleton class. My goal is that any subclasses just have to implement the init() function AND NOTHING ELSE. Here is what I did:

template <typename T>
class Singleton
{
    public: 

        Singleton()
        {
            init();
        }

        static T& instance()
        {
            static T instance;
            return instance;
        }

    protected:
        virtual void init() = 0;   
};

class SubSingleton : public Singleton<SubSingleton>
{
    protected: 
        void init()
        {
            cout << "Init SubSingleton" << endl;
        }
};

This won’t compile, since init() is protected and can’t be called from a public static function. There are 2 solutions to this problem. First we can make the init() function public, but I don’t want to expose this function publicly. So this only leaves the second solution, change the subclass as follows:

class SubSingleton : public Singleton<SubSingleton>
{
    friend class Singleton<SubSingleton>;

    protected:

        void init()
        {
            cout << "Init SubSingleton" << endl;
        }
};

This works perfectly, but I don’t want the friend statement, since other programmers might extend my code and might not know that this should be added.

Is there any other way of implementing this without the friend statement? Maybe anything from Andrei Alexandrescu?

EDIT: The init function is now called in the constructor instead of the instance() function.

EDIT2: For technical reasons and compatibility I need an init() function and can’t just do the initialization in the constructor.

The solution for casting works, but if I call init() from the constructor the casting doesn’t work anymore. Any suggestions?

  • 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-07T13:41:25+00:00Added an answer on June 7, 2026 at 1:41 pm

    Problem is that you do not use the virtual function as you intend to use. i,e there no polymorphism in action now. Why? because you call init() on the direct derived object. The use of init() function being virtual is when you invoke init() either on the base-class reference or on base class pointer as given below. Then the invocation happens from the base-class scope and since instance() method is a base class method, calling a protected method from that is perfectly fine.

        static T& instance()
        {
            static T myInstance;
            Singleton<T>& t = myInstance;  // Just define a dummy reference here.
            t.init();
            return myInstance;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class and want to implement a function in the
I have never done any Singleton class before and now I figured that for
I have a singleton class that implements two other abstract classes. My monkey::getMonkey fails
I have an abstract base class called Party. There are several concrete subclasses (Company,
I have a abstract base C# class with a couple of methods that has
I have an abstract base class called Curve . There are three classes that
I have one main interface and an abstract class implementing all derivable methods (that
I have a simple question. I use a singleton which implements an abstract class.
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
Let say I have abstract class called: Tenant and Customer. The tenant in this

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.