この質問は、私がここで尋ねた以前の質問に由来します。外部ライブラリや C++ 11 仕様を使用できません。つまり、std::bind、std::function、boost::bind、boost::function などは使用できません。自分で作成する必要があります。問題は次のとおりです。
次のコードを検討してください。
編集
要求どおりに問題を示す完全なプログラムを次に示します。
#include <map>
#include <iostream>
class Command {
public:
virtual void executeCommand() = 0;
};
class Functor {
public:
virtual Command * operator()()=0;
};
template <class T> class Function : public Functor {
private:
Command * (T::*fptr);
T* obj;
public:
Function(T* obj, Command * (T::*fptr)()):obj(obj),
fptr(fptr) {}
virtual Command * operator()(){
(*obj.*fptr)();
}
};
class Addition:public Command {
public:
virtual void executeCommand(){
int x;
int y;
x + y;
}
};
class CommandFactory {
public:
virtual Addition * createAdditionCommand() = 0;
};
class StackCommandFactory: public CommandFactory {
private:
Addition * add;
public:
StackCommandFactory():add(new Addition()) {}
virtual Addition * createAdditionCommand(){
return add;
}
};
void Foo(CommandFactory & fact) {
Function<CommandFactory> bar(&fact,&CommandFactory::createAdditionCommand);
}
int main() {
StackCommandFactory fact;
Foo(fact);
return 0;
}
それが与えるエラーは"no instance of constructor "Function<T>::Function [with T=CommandFactory] matches the argument list, argument types are: (CommandFactory *, Addition * (CommandFactory::*)())
派生型を渡しているので、文句を言っていると思います。fact
後で StackCommandFactory ではない可能性があるため、抽象クラスへのポインター/参照を使用する必要があります。
私は言うことができません:
void Foo(CommandFactory & fact){
Function<CommandFactory> spf(&fact,&fact.createAdditionCommand); //error C2276
}
そのため、エラーC2276が表示されます(リンク先の質問のように)'&' : illegal operation on bound member function expression.
つまり、私の質問は次のとおりです。「このファンクター オブジェクトを初期化して、上記のインターフェイスで使用できるようにするにはどうすればよいですか?」