Entschuldigung für den Titel, ich weiß nicht, welchen passenden Titel dieses Thema. Ich habe diesen Test-Code http://ideone.com/V8h5K den als Compiler-Fehler, und ich weiß nicht, was mit diesem Code falsch:C++ Syntax err, abstrahiert statische und Member-Funktion in eine Template-Klasse
meine Absicht ist, eine Abstraktion zu schaffen, so dass ich einen Container generischer Funktionszeiger erstellen kann, den don Es ist egal, ob der Funktionszeiger ein Zeiger auf statische Funktion oder eine Elementfunktion ist.
#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;
}
Es kann hilfreich sein, den Fehlertext anzuzeigen und die Zeilennummer anzugeben. – sje397
@ sj397: Sie können hier sehen: http://ideone.com/V8h5K – uray