동일한 유형의 콜백 함수를 사용하는 클래스 템플릿을 정의하려고합니다. 같은 뭔가 :클래스 템플릿에 대한 콜백 함수 템플릿 정의
typedef template<class T> bool CallbackFn(T x);
template<class T> class MyClass
{
public:
MyClass() {}
~MyClass() {}
void addCallbackFn(CallbackFn* fn) { callbackFn = fn; }
private:
CallbackFn* callbackFn;
};
그리고 그것은 다음과 같이 사용됩니다 :
bool testFunctionInt(int x) { return true; }
bool testFunctionString(std::string x) { return true; }
MyClass<int> a;
a.addCallbackFn(testFunctionInt);
MyClass<std::string> b;
b.addCallbackFn(testFunctionString);
불행하게도 콜백 기능이
typedef
를 통해 기능 템플릿으로 정의 할 수 없습니다.
다른 방법이 있습니까?
간단한 해결책을 알려 주셔서 감사합니다. – jensph