对不起标题,我不知道这个主题的标题是什么。 我有这个测试代码http://ideone.com/V8h5K作为编译错误, 我不知道这段代码有什么问题:
我的目的是创建一个抽象,以便我可以创建一个容器 泛型函数指针,它不关心函数指针是否为指针 静态函数或成员函数。
#include <stdio.h>
#include <vector>
template<typename R=void,
typename A=void,
typename F=R (*)(A)> class Method {
protected: F method;
public : Method(F methodPtr):method(methodPtr){ };
virtual R operator()(A argument) {
return this->method(argument);
};
typedef F FuncType;
};
template<typename A, typename F> class Method<void,A,F> {
protected: F method;
public : Method(F methodPtr) :method(methodPtr) { };
virtual void operator()(A argument) {
this->method(argument);
};
typedef F FuncType;
};
template<typename R, typename F> class Method<R,void,F> {
protected: F method;
public : Method(F methodPtr) :method(methodPtr) { };
virtual R operator()() {
return this->method();
};
typedef F FuncType;
};
template<typename F> class Method<void,void,F> {
protected: F method;
public : Method(F methodPtr) :method(methodPtr) { };
virtual void operator()() {
this->method();
};
typedef F FuncType;
};
template<typename C=void,
typename R=void,
typename A=void,
typename F=R (C::*)(A)>
class ClassMethod : public Method<R,A,F> {
protected: C& owner;
public : ClassMethod(C& methodOwner,F methodPtr)
:Method<R,A,F>(methodPtr),owner(methodOwner){ };
virtual R operator()(A argument) {
return ((this->owner).*(this->method))(argument);
};
typedef F FuncType;
};
template<typename C, typename A, typename F>
class ClassMethod<C,void,A,F>: public Method<void,A,F> {
protected: C& owner;
public : ClassMethod(C& methodOwner,F methodPtr)
:Method<void,A,F>(methodPtr),owner(methodOwner){ };
virtual void operator()(A argument) {
((this->owner).*(this->method))(argument);
};
typedef F FuncType;
};
template<typename C, typename R, typename F>
class ClassMethod<C,R,void,F>: public Method<R,void,F> {
protected: C& owner;
public : ClassMethod(C& methodOwner,F methodPtr)
:Method<R,void,F>(methodPtr),owner(methodOwner){ };
virtual R operator()() {
return ((this->owner).*(this->method))();
};
typedef F FuncType;
};
template<typename C, typename F>
class ClassMethod<C,void,void,F>: public Method<void,void,F> {
protected: C& owner;
public : ClassMethod(C& methodOwner,F methodPtr)
:Method<void,void,F>(methodPtr),owner(methodOwner){ };
virtual void operator()() {
((this->owner).*(this->method))();
};
typedef F FuncType;
};
// ---- implementation -----
template<typename A> class MethodList {
protected:
std::vector< Method<void,A> > methods;
public:
void add(typename Method<void,A>::FuncType fp) {
this->methods.push_back(Method<void,A>(fp));
}
template<class C> void add(typename C& instance,
typename ClassMethod<C,void,A>::FuncType fp) {
this->methods.push_back(ClassMethod<C,void,A>(instance,fp));
}
void invoke(A argument) {
typename std::vector< Method<void,A> >::iterator it;
for(it=this->methods.begin() ; it!=this->methods.end() ; it++) {
(*it)(argument);
}
}
};
void function1(int arg) {
printf("function1(%d)",arg);
}
class Class1 {
public:
void function1(int arg) {
printf("Class1::function1(%d)",arg);
}
};
int main(int argc,char* argv[] )
{
Class1 inst;
MethodList<int> methodList;
methodList.add(function1);
methodList.add<Class1>(inst,&Class1::function1);
methodList.invoke(123);
return 0;
}
答案 0 :(得分:0)
只要在大多数实现上实例化封闭类,就会实例化此虚拟成员函数。您不必使用它来进行实例化:
virtual R operator()(A argument) {
return this->method(argument);
};
但是当F
为R(C::*)(Args)
时,this->method
的使用将会形成错误。所以这种推导是错误的:
// will instantiate Method<R, A, F>
class ClassMethod : public Method<R,A,F>
FWIW您不需要专注于R
void
。你可以在void函数中使用一个类型为void
的表达式的return语句。
// works fine with R being void
virtual R operator()(A argument) {
return static_cast<R>(this->method(argument));
};
哇,现在我看到你也在切换MethodList
类模板中的对象。如果您不知道“切片”是什么,我建议您在编写此类代码之前先阅读基本的C ++主题。