【发布时间】:2019-10-16 10:54:43
【问题描述】:
如果我有以下设置,只是示例。我遇到的问题是让模板的连接功能起作用。
class A
{
public:
Delegate<void(int)> test;
};
class B
{
public:
void test(int a)
{
std::cout << a;
};
};
我想将此与以下内容联系起来:
a.test.connect(&b, &B::test);
然后类a的实例可以调用class B实例上的测试函数。
我有以下模板,但无法捕获我需要的实例和函数。
#ifndef DELEGATE_H
#define DELEGATE_H
#include <functional>
template<class _Delegate>
class Delegate;
template<class R, class... Args>
class Delegate<R(Args...)>
{
public:
template<typename T>
void connect(T* t, R(Args... ) )
{
mFunction = std::function(T::*t, R(Args...));;
}
R operator()(Args... args)
{
return mFunction(args...);
}
protected:
std::function<R(Args...)> mFunction;
};
#endif
【问题讨论】:
-
请提供包含错误消息的minimal reproducible example。您的代码中有很多错误,例如
Class A,B::test是私有的,在定义类B后缺少分号,...
标签: c++ templates delegates variadic-templates pointer-to-member