派生类型的自动静态调用 [英] Automatic static invocation of derived types

查看:119
本文介绍了派生类型的自动静态调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一种方法让派生类用模板类型自动实例化一个静态变量(这要么不需要派生类的作者的任何东西,要么强迫他调用这个静态方法,派生类定义有效)。

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.

基本上我有一个全局工厂类,有一个模板化函数registerType。对于从Entity派生的每个类,我需要使用派生类型的模板参数调用此函数。目前,我不得不在一些init函数中手动做,这导致对这个函数的一个大块的调用,这是违反模板的原则。

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.

所以我有这个:

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
}

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;
};

注册商的构造函数

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

和派生类型

class CrateFactory : public AbstractFactory<Entity, CrateFactory>
{
    public:
        CrateFactory(FactoryLoader* loader);
        virtual ~CrateFactory();
        Entity* useFactory(FactoryParameters* parameters);
        static std::string name()
        {
            return "CrateFactory";
        }


推荐答案

如果任何人仍然感兴趣,我想到了。静态模板成员变量不会自动实例化,除非使用它们。我需要在构造函数被调用之前被实例化,所以我不能使它成为一个静态局部。解决方案是使它成为一个静态模板成员变量,然后在成员函数(我使用构造函数)中使用它(只要调用一个空函数就可以了)。这迫使编译器为每个已经声明的模板参数实例化静态,因为实例化的构造函数代码使用它,例如:

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:

我的注册表类,调用

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;
};

注册商的构造函数

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

和我的类构造函数

template <typename Product, typename DerivedType>
AbstractFactory::AbstractFactory()
{
    registrar.check();
}

这篇关于派生类型的自动静态调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆