Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于创建信号对象的函子+成员指针_C++_Functor_Member Pointers - Fatal编程技术网

C++ 用于创建信号对象的函子+成员指针

C++ 用于创建信号对象的函子+成员指针,c++,functor,member-pointers,C++,Functor,Member Pointers,我有以下代码: // signal supporter parent class signalable {}; template <class typeT = signalable> typedef void (typeT::*trig)(std::string); template <class typeT = signalable> class trigger { private: typeT* instance; type

我有以下代码:

// signal supporter parent
class signalable {};

template <class typeT = signalable>
typedef void (typeT::*trig)(std::string);

template <class typeT = signalable>
class trigger
{
    private:
        typeT* instance;
        typeT::trig fun;

    public:
        trigger(typeT* inst, typeT::trig function)
            : instance(inst), fun(function)
        {}
        void operator ()(std::string param)
        {
            (instance->*fun)(param);
        }
};
我得到了很多编译错误,我打赌专业人士都知道。我只是对这个背景有点困惑

我想做的很清楚:将指针传递给一个对象,并将指针传递给它的一个成员函数,以生成一个函子并在我的程序中传递它

非常感谢您的帮助和纠正


谢谢大家!

你想做这样的事吗

#include <string>
#include <iostream>

// signal supporter parent
class signalable 
{
public:
    void foo(std::string s) { std::cout << "hello: " << s << std::endl; }
};


template <class typeT = signalable>
class trigger
{

    typedef void (typeT::*trig)(std::string);

    private:
        typeT* instance;
        trig fun;

    public:
        trigger(typeT* inst, trig function)
            : instance(inst), fun(function)
        {}
        void operator ()(std::string param)
        {
            (instance->*fun)(param);
        }
};

int main()
{
    signalable s;
    trigger<> t(&s, &signalable::foo);
    t("world");
}
至于代码中的一些更具体的错误,其中大部分似乎与typedef有关。C++11允许模板typedef,但它们看起来不像那样。查看此线程以获取模板typedefs的示例:


你的代码是正确的,本。只需将成员fun和构造函数的typeT::trig更改为trig即可解决此问题。谢谢。Ben C回答了你的问题,但你可能想研究一下,寻找一种很好的方法来解决我认为是你的原始问题。@smocking我理解你的观点,我以前处理过这个案例,但在这个案例中,观察者模式是我的目标,因为在我的代码中,事件将以链式方式传播,而不是将某些事件传播到多个被调用方。无论如何,谢谢你帮我观察对比。我不确定我是否理解你为什么想要那种类型的信号,但是你不能通过让你的链接信号类同时从观察者和可观察接口继承来扩展一点模式吗?我想这一定是正确的方法,虽然在我正在进行的这个非常小的项目中,有一半以上的概念是硬编码的,但应用这样的企业级解决方案并没有那么重要。我也很欣赏你的评论。谢谢