<bdo id='SeSCr'></bdo><ul id='SeSCr'></ul>

  1. <small id='SeSCr'></small><noframes id='SeSCr'>

    <legend id='SeSCr'><style id='SeSCr'><dir id='SeSCr'><q id='SeSCr'></q></dir></style></legend>
    1. <tfoot id='SeSCr'></tfoot>
    2. <i id='SeSCr'><tr id='SeSCr'><dt id='SeSCr'><q id='SeSCr'><span id='SeSCr'><b id='SeSCr'><form id='SeSCr'><ins id='SeSCr'></ins><ul id='SeSCr'></ul><sub id='SeSCr'></sub></form><legend id='SeSCr'></legend><bdo id='SeSCr'><pre id='SeSCr'><center id='SeSCr'></center></pre></bdo></b><th id='SeSCr'></th></span></q></dt></tr></i><div id='SeSCr'><tfoot id='SeSCr'></tfoot><dl id='SeSCr'><fieldset id='SeSCr'></fieldset></dl></div>

      在对象工厂中注册对象创建者

      时间:2023-10-07

            <tbody id='E2mJE'></tbody>
        • <small id='E2mJE'></small><noframes id='E2mJE'>

            <bdo id='E2mJE'></bdo><ul id='E2mJE'></ul>

              <legend id='E2mJE'><style id='E2mJE'><dir id='E2mJE'><q id='E2mJE'></q></dir></style></legend>

                <tfoot id='E2mJE'></tfoot>
              1. <i id='E2mJE'><tr id='E2mJE'><dt id='E2mJE'><q id='E2mJE'><span id='E2mJE'><b id='E2mJE'><form id='E2mJE'><ins id='E2mJE'></ins><ul id='E2mJE'></ul><sub id='E2mJE'></sub></form><legend id='E2mJE'></legend><bdo id='E2mJE'><pre id='E2mJE'><center id='E2mJE'></center></pre></bdo></b><th id='E2mJE'></th></span></q></dt></tr></i><div id='E2mJE'><tfoot id='E2mJE'></tfoot><dl id='E2mJE'><fieldset id='E2mJE'></fieldset></dl></div>
                本文介绍了在对象工厂中注册对象创建者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个方便的对象工厂模板,它可以通过类型 ID 名称创建对象.实现非常明显:ObjectFactory 包含从 std::string 到对象创建者函数的映射.那么所有要创建的对象都要在这个工厂注册.

                I have the convenient object factory template that creates objects by their type id names. The implementation is pretty obvious: ObjectFactory contains the map from std::string to object creator function. Then all objects to be created shall be registered in this factory.

                我使用以下宏来做到这一点:

                I use the following macro to do that:

                #define REGISTER_CLASS(className, interfaceName) 
                   class className; 
                   static RegisterClass<className, interfaceName> regInFactory##className; 
                   class className : public interfaceName
                

                RegisterClass 在哪里

                   template<class T, class I>
                   struct RegisterClass
                   {
                      RegisterClass()
                      {
                         ObjectFactory<I>::GetInstance().Register<T>();
                      }
                   };
                

                用法

                class IFoo
                {
                public:
                   virtual Do() = 0;
                   virtual ~IFoo() {}
                }
                
                REGISTER_CLASS(Foo, IFoo)
                {
                   virtual Do() { /* do something */ }
                }
                
                REGISTER_CLASS(Bar, IFoo)
                {
                   virtual Do() { /* do something else */ }
                }
                

                在工厂中同时定义和注册类.

                Classes are defined and registered in factory simultaneously.

                问题在于 regInFactory... 静态对象是在 .h 文件中定义的,因此它们将被添加到每个翻译单元中.同一个对象创建者会被多次注册,更重要的是会有很多静态存储时长的冗余对象.

                The problem is that regInFactory... static objects are defined in .h files, so they will be added to every translation unit. The same object creator will be registered several times, and, more important, there will be a lot of redundant objects with static storage duration.

                有什么办法可以做到如此优雅的注册(不是复制/粘贴类和接口名称),但又不会在全球范围内传播冗余的静态对象?

                Is there any way to do such elegant registration (not to copy/paste class and interface names), but do not spread redundant static objects around the globe?

                如果一个好的解决方案需要一些 VC++ 特定的扩展(不符合 C++ 标准),我会接受.

                If a good solution needs some VC++ specific extensions (not conforming to C++ standard), I will be OK with that.

                推荐答案

                所以你想把变量定义放在头文件中?有一种可移植的方式:模板类的静态变量.所以我们得到:

                So you want to put variables definitions in header file? There is a portable way: static variables of template classes. So we get:

                template <typename T, typename I>
                struct AutoRegister: public I
                {
                    // a constructor which use ourRegisterer so that it is instantiated; 
                    // problem catched by bocco
                    AutoRegister() { &ourRegisterer; } 
                private:
                    static RegisterClass<T, I> ourRegisterer;
                };
                
                template <typename T, typename I>
                RegisterClass<T, I> AutoRegister<T, I>::ourRegisterer;
                
                class Foo: AutoRegister<Foo, IFoo>
                {
                public:
                    virtual void Do();         
                };
                

                请注意,我保留了 IFoo 非虚拟的继承.如果存在多次从该类继承的风险,则它应该是虚拟的.

                Note that I've kept the inheritance of IFoo non virtual. If there is any risk of inheriting from that class multiple times, it should be virtual.

                这篇关于在对象工厂中注册对象创建者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++中的观察者设计模式 下一篇:如何强制子虚拟函数首先调用其父虚拟函数

                相关文章

                最新文章

                • <bdo id='efxL9'></bdo><ul id='efxL9'></ul>
              2. <small id='efxL9'></small><noframes id='efxL9'>

                  <tfoot id='efxL9'></tfoot>
                  <i id='efxL9'><tr id='efxL9'><dt id='efxL9'><q id='efxL9'><span id='efxL9'><b id='efxL9'><form id='efxL9'><ins id='efxL9'></ins><ul id='efxL9'></ul><sub id='efxL9'></sub></form><legend id='efxL9'></legend><bdo id='efxL9'><pre id='efxL9'><center id='efxL9'></center></pre></bdo></b><th id='efxL9'></th></span></q></dt></tr></i><div id='efxL9'><tfoot id='efxL9'></tfoot><dl id='efxL9'><fieldset id='efxL9'></fieldset></dl></div>

                    <legend id='efxL9'><style id='efxL9'><dir id='efxL9'><q id='efxL9'></q></dir></style></legend>