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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:27:43+00:00 2026-05-23T09:27:43+00:00

Does anyone know of a way to make derived classes automatically instantiate a static

  • 0

Does anyone know of a way to make derived classes automatically instantiate a static variable with a template type (this either has to require nothing from the writer of the derived class, or force him to call this static method in order to make the derived class definition valid).

This is probably impossible to understand, I’ll try and define it better.

Basically I have a global factory class with a templated function called registerType. For every class derived from Entity, I need this function to be called with the template parameter of the derived type. At the moment, I have to manually do it in some init function, which results in a large block of calls to this function, which kind of goes against the principle of templates for me.

So I have this:

class Factory
{
  template <typename EntityType>
  registerEntityType();
};

void someInitFunction()
{
   /// All of these are derived from Entity
  gFactory.registerEntityType<EntityType1>();
  gFactory.registerEntityType<EntityType2>();
  gFactory.registerEntityType<EntityType3>();
  /// and so on
}

whereas I would rather have this:

class Factory
{
  template <typename EntityType>
  registerEntityType();
};

class Entity // Abstract
{
    /// This function should be called automatically with the derived 
    /// type as a parameter
    SomeStaticConstructor<MDerivedType>() 
    {
      gFactory.registerEntityType<MDerivedType>();
    }
};

EDIT: This is the static recurring template code that isn’t working:

This is my base class, and the class for automatically registering stuff

template <typename DerivedType>
class Registrar
{
    public:
        Registrar();
        void check();
};
template <typename Product, typename DerivedType>
class AbstractFactory: public AbstractFactoryBase<Product>
{
    public:
        AbstractFactory();
        ~AbstractFactory();
    private:
        static Registrar<DerivedType> registrar;
};

The registrar’s constructor

template <typename DerivedType>
Registrar<DerivedType>::Registrar()
{
    std::cout << DerivedType::name() << " initialisation" << std::endl;
    g_AbstractFactories.registerFactoryType<DerivedType>(DerivedType::name());
}

And a derived type

class CrateFactory : public AbstractFactory<Entity, CrateFactory>
{
    public:
        CrateFactory(FactoryLoader* loader);
        virtual ~CrateFactory();
        Entity* useFactory(FactoryParameters* parameters);
        static std::string name()
        {
            return "CrateFactory";
        }
  • 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-23T09:27:44+00:00Added an answer on May 23, 2026 at 9:27 am

    If anyone is still interested, I figured it out. Static template member variables are not automatically instantiated unless they are used. I needed it to be instantiated before the constructor was called, so I couldn’t make it a static local. The solution is to make it a static template member variable, and then use it (just call an empty function on it if you want) in a member function (I use the constructor). This forces the compiler to instantiate the static for every template parameter ever declared, because the instantiated constructor code uses it, for example:

    My registry class, with its blank function for calling

    template <typename DerivedType>
    class Registrar
    {
        public:
            Registrar();
            void check(){}
    };
    

    My class I want registered.

    template <typename Product, typename DerivedType>
    class AbstractFactory: public AbstractFactoryBase<Product>
    {
        public:
            AbstractFactory();
            ~AbstractFactory();
        private:
            static Registrar<DerivedType> registrar;
    };
    

    The registrar’s constructor

    template <typename DerivedType>
    Registrar<DerivedType>::Registrar()
    {
        std::cout << DerivedType::name() << " initialisation" << std::endl;
        g_AbstractFactories.registerFactoryType<DerivedType>(DerivedType::name());
    }
    

    And my classes constructor

    template <typename Product, typename DerivedType>
    AbstractFactory::AbstractFactory()
    {
        registrar.check();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know a way to intercept the call Hibernate will make to instantiate
Does anyone know of a way to make an AIR app display over top
Does anyone know if there is a simple way to make images snap to
Does anyone know of any way to make a datasource configuration change in WAS
Does anyone know a way to detect if a button with this, code below,
Does anyone know a way to make an asp button... <asp:button runat=server>Button1</asp:button> resemble an
does anyone know whether there is a way to make an image grayscale on
Does anyone know if there is a way to make an Excel 2003 Document
Does anyone know a way to auto-generate database tables for a given class? I'm
Does anyone know a way to find out programatically which physical disk holds a

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.