[질문:c++]template에서 함수 포인터 사용..

ftfuture의 이미지

#include <iostream>
 
using namespace std;
 
typedef bool (*Handler)( int index_, void *dummy_ );
 
template<class typeArg>
class _testTemplate {
public:
        int iTem;
 
        void _TEST( Handler Handler_, void *dummy_ );
};
 
template<class typeArg>
void _testTemplate<typeArg>::_TEST( Handler Handler_, void *dummy_ )
{
        Handler_( 1, dummy_);
}
 
class _testBody {
public:
        bool _test_( int index, void *dummy_ );
};
 
bool _testBody::_test_( int index, void *dummy_ )
{
        int i;
 
        cout << "test" << endl;
 
        return true;
}
 
 
 
int main( void )
{
        class _testTemplate<int> testone;
        class _testBody testBody;
 
        testone._TEST( testBody._test_(1, NULL), NULL );
}

함수 포인터가 클래스의 멤버 변수가 되니 에러가 나네요..
결과

g++ -o template_functor_test template_functor_test.cpp
template_functor_test.cpp: In function ‘int main()’:
template_functor_test.cpp:43: error: no matching function for call to ‘_testTemplate<int>::_TEST(bool, NULL)’
template_functor_test.cpp:16: note: candidates are: void _testTemplate<typeArg>::_TEST(bool (*)(int, void*), void*) [with typeArg = int]
익명사용자의 이미지

멤버 함수 포인터와 일반 함수 포인터가 다릅니다.

멤버함수에 대한 typedef 은

typedef bool (_testBody::*MHandler)(int index_, void *dummy_);

같이 하셔야 되고, instance 된 객체가 없이 멤버함수 포인터를 쓸 수 없습니다. (static 제외)

Handler 부분도 template 으로 일반화 시키셔야 될텐데, Modern C++ Design 의

Functor 부분을 보시면 많은 도움이 되실겁니다.

ftfuture의 이미지

넵 감사합니다.
일단 testBody._test_ 함수를 _test_ 전역 함수로 만들어서 해결했습니다.
다른 방법은 없는지요.