2011-11-22 3 views
1

boost :: bind를 사용하여 함수 포인터를 전달하려고합니다.boost 콜백 함수 포인터를 매개 변수로 boost

void 
Class::ThreadFunction(Type(*callbackFunc)(message_type::ptr&)) 
{ 
} 

boost::shared_ptr<boost::thread> 
Class::Init(Type(*callbackFunc)(message_type::ptr&)) 
{ 
    return boost::shared_ptr<boost::thread> (
     new boost::thread(boost::bind(&Class::ThreadFunction, callbackFunc)) 
     ); 
} 

나는 다음과 같은 오류가 얻을 : 그러나

1>C:\dev\sapphire\boost_1_46_1\boost/bind/mem_fn.hpp(362) : warning C4180: qualifier applied to function type has no meaning; ignored 
1>C:\dev\sapphire\boost_1_46_1\boost/bind/mem_fn.hpp(333) : error C2296: '->*' : illegal, left operand has type 'Type (__cdecl **)(message_type::ptr &)' 

를, 내가 다음에 변경할 수 있었다, 그것을 잘 작동합니다 :

void 
ThreadFunction(Type(*callbackFunc)(message_type::ptr&)) 
{ 
} 

boost::shared_ptr<boost::thread> 
Class::Init(Type(*callbackFunc)(message_type::ptr&)) 
{ 
    return boost::shared_ptr<boost::thread> (
     new boost::thread(boost::bind(&ThreadFunction, callbackFunc)) 
     ); 
} 

이유는 이러한 오류 나는 경우를받을 수 있나요 Class 클래스에서 메서드를 선언합니까?

답변

3

정적이 아닌 멤버 함수를 바인딩 할 때 사용할 포인터 this을 제공해야합니다. 함수가 Class의 특정 인스턴스와 관련되지 않게하려면 함수를 정적으로 설정해야합니다.

struct Class { 
    void foo(int) { } 
    static void bar(int) { } 
}; 

std::bind(&Class::foo, 5); // Illegal, what instance of Class is foo being called 
          // on? 

Class myClass; 
std::bind(&Class::foo, &myClass, 5); // Legal, foo is being called on myClass. 

std::bind(&Class::bar, 5); // Legal, bar is static, so no this pointer is needed. 
+1

+1, 멤버 함수 사용에 대한 대안을 언급하는 것을 잊어 버렸습니다. – Anthony

2

Class의 인스턴스도 바인딩해야하기 때문에. Boost documentation을 읽으십시오.

boost::thread(boost::bind(
    &Class::ThreadFunction, &someClassInstance, _1), 
    callbackFunc); 

참고 : 코드 내 머리 위로 떨어져입니다 이상이 필요

는 생각 . 나는 그것이 정확하다고 생각한다.

+0

충분하지만 자리 표시 자 이름없는 네임 스페이스에 있습니다. –

+0

@dauphic, 감사합니다. – Anthony

관련 문제