English 中文(简体)
c++ 工厂类型
原标题:c++ factory pattern

我很抱歉,但我要提出我试图解决这一问题以及实际问题。 Maybe 我要指出另一种可能更好的办法。

我愿建立一个错开的手递系统。 我将有几个手递模块。 基本想法是,在初始阶段,这些单元将在活动清单中登记其处理的活动(登记、登记、登记)。 如果触发了一场活动,则将对该活动清单进行研究,并创建和管理所有列出的单元。 为了实现这一点,所有单元都有一个接口。

我的两个问题是:

  1. 自2006年以来 我只想在实际活动中立即介绍这些单元,我要么需要一种办法,向建筑商(“虚拟建筑商”)储存一个点子,因为我知道,没有虚拟建筑商,我还要加上一个工厂方法。 我知道的传统做法是:

    class myInterface {
    public:
       ...
       static virtual *myInterface builder() = 0; //is static (pure) virtual possible?
    };
    
    class example : public myInterface {
    public:
       ...
       static virtual *myInterface builder() { return new example(); }
    };
    

    I would then have to store pointers to the builder() functions in the event list and execute the functions in case the event occurs to create the objects. I am wondering if there is a way arround writing static virtual *myInterface builder() { return new example(); } for every example module. A template approach would be to put it like this:

    template static virtual *myInterface builder() { return new T(); }

    然而,我非常喜欢,因为我必须写以下文字,为一项活动登记一个单元:

    登记册(eventName,builder())

    考虑到我只希望在每个模块中设立一个静态登记功能:

    class example: public myInterface {
        static void registerMe {
            register(event1,builder<example>());
            register(event2,builder<example>());
            ...
            register(eventn,builder<example>());
        }
    };
    

    it would be way more convenient if a call like register(event) would be sufficient. i could add register(event) to the interface, but I dont want to rewrite the register function for every template which would read

    void register(event) { getListForEvent(event).pushBack(builder<example>()); }
    

    (建筑工序瞬时参数各不相同),使其成为一个模板本身并不好,因为这使我重新撰写了<条码>register<myster>随附。

    So is there a more conviniant way? (During the pondering and writing of this I came up with inheriting from a template class. If polimorphism works with template classes with different instantiation parameters. Otherwise I have to use an Interface class, wrap it in a template and Inherit from that )

  2. 第二个问题是,我有来自我的接口类别的不同类别。 然而,他们的成员在接口中没有定义。 我要向成员职能储存一个职能点,我如何这样做? 这种类型必须接受不同的基类,但功能签名的其余部分相同(见下文)。

    class A : public interface {
         void myFunc()
    };
    
    class A : public interface {
         void yourFunc()
    };
    
    whatType* a = &A::myFunc;
    whatType* b = &B::yourFunc;
    
最佳回答

静态(纯度)几乎有可能?

CRTP(频繁出现的模板模式)至救援

template <class Derived>
class myInterface {
public:
    ...
    virtual *myInterface builder() = 0; 

    Derived* buildDerived() 
    { 
         return dynamic_cast<Derived*>(builder());
    }

};
问题回答
  1. 法定成员的职能并不以ts为第一个参数,而非法定成员的职能是这样做的,因此,固定成员的职能可视为特定名称空间的全球职能。 正因为如此,请在<>上注明>。

    a. 静态虚拟构体 *

  2. 无法界定这种职能类型。 如果在不同级别有职能,但具有相同签名,则认为有新的接口继承了你的第一种接口,并将虚拟成员加入新的接口。 当你找到一个基调界面点时,使用dynamic_cast<>strong>,从基调接口点获得原类型点,然后打电话给新的成员功能。 候补成员:Boost:Bind。 这似乎有助于你的处境。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?