FastDelegate指的是http://www.codeproject.com/KB/cpp/FastDelegate.aspx,但我认为它不相关。

我有以下代码,但出现错误。

#include <FastDelegate.h>


using namespace fastdelegate;

template <typename T>
T Getter() {}

template <typename T>
void Setter(T) {}

template <typename T>
class Prop
{
public:
    typedef FastDelegate0<T> Getter;
    typedef FastDelegate1<T> Setter;

    Prop(Getter getter, Setter setter) :
        m_Getter(getter), m_Setter(setter)
    {

    }

private:
    Getter m_Getter;
    Setter m_Setter;
};

template <typename T>
inline Prop<T>* MakeProp(FastDelegate0<T> getter, FastDelegate1<T> setter)
{
    return new Prop<T>(getter, setter);
}

static int Target = 0;
int main()
{
    FastDelegate0<int> fdGetter(Getter<int>);
    Prop<int>* c = MakeProp(fdGetter, Setter<int>);
    // ^^^^ error: no matching function for call to 'MakeProp'
}


如果将main()更改为:

int main()
{
    FastDelegate0<int> fdGetter(Getter<int>);
    FastDelegate1<int> fdSetter(Setter<int>);
    Prop<int>* c = MakeProp(fdGetter, fdSetter); // It works.
}


要么:

int main()
{
    FastDelegate0<int> fdGetter(Getter<int>);
    Prop<int>* c = MakeProp<int>(fdGetter, Setter<int>); // It works, too.
}


我认为,MakeProp()应该从fdGetter(即T,而不是自动调用int的构造函数)中获取FastDelegate1<int>。但这不是。为什么?

附言我想将getter和setter保存在Prop中,欢迎对此方法提出任何建议。在函数中传递参数的过程中,复制FastDelegate *的实例可能不好。

最佳答案

你有没有尝试过

Prop<int>* c = MakeProp(FastDelegate0<int>(Getter<int>), FastDelegate1<int>(Setter<int>));




Setter<int>不能转换为FastDelegate1<T>

09-10 04:40