我有 3 节课,如下所示:
template <typename RET, typename ARG1 = int, [...], typename ARG5 = int>
class FunctionPointer {
virtual operator()(ARG1, [...], ARG5);
};
template <typename RET, class TYPE, typename ARG1 = int, [...], typename ARG5 = int>
class ClassFunctionPointer : public FunctionPointer<RET, ARG1, [...], ARG5> {
// make it so that class functions fit here
};
template <typename RET, typename ARG1 = int, [...], typename ARG5 = int>
class GlobalFunctionPointer : public FunctionPointer<RET, ARG1, [...], ARG5> {
// make it so that non-member functions fit here
};
它们都被重载,因此每个类都可以使用可变数量的参数进行实例化,这意味着以下所有示例都是有效的:
GlobalFunctionPointer<void> a(&test1);
GlobalFunctionPointer<void, int> b(&test2);
ClassFunctionPointer<void, TestClass, int, char> c(testObject, &TestClass::test1);
ClassFunctionPointer<void, TestClass, int, char, std::string, float, SomeClass> d(testObject, &TestClass::test2);
但是对于这一切,我写了很多代码,大约 500 行,见这里。我发现这很难看,所以我正在寻找一个更优雅的解决方案。有吗?
(顺便说一句,所有纠结的代码都是为了成为更复杂的事件系统的基础。)