10
A
답변
15
두 숨겨진 매개 변수 각각 self
및 유형 id
의 _cmd
및 SEL
와 그냥 일반 C 함수는 메시지 구현 (IMP
)에 결합 할 수 있어야한다.
EDIT : 다음 예제를 테스트하면 정상적으로 작동하는 것 같습니다.
그누 스텝으로#include <stdio.h>
#include <boost/bind.hpp>
#include <Foundation/NSObject.h>
@interface MyClass : NSObject
{
}
-(int) doSomething:(int)arg;
@end
@implementation MyClass
-(int) doSomething:(int)arg
{
printf("doSomething: self=0x%08x _cmd=0x%08x\n", self, _cmd);
return arg + 1;
}
@end
int main(void)
{
MyClass *myObj = [[MyClass alloc] init], *otherObj = [[MyClass alloc] init];
typedef int (*MyFunc)(id, SEL, int);
SEL doSomething_sel = @selector(doSomething:);
MyFunc doSomething_impl = (MyFunc)[myObj methodForSelector:doSomething_sel];
// bind self & _cmd arguments:
// calls [myObj doSomething:x]
int result = boost::bind(doSomething_impl, myObj, doSomething_sel, _1)(14);
printf("result1: %d\n", result);
// bind _cmd & arg:
// calls [otherObj doSomething:3]
result = boost::bind(doSomething_impl, _1, doSomething_sel, 42)(otherObj);
printf("result2: %d\n", result);
return 0;
}
, 같은 컴파일 :
gcc objcbind.mm -o objcbind -I/usr/include/GNUstep -lobjc -lstdc++ -lgnustep-base
를 맥 OS X에서와 같은 컴파일 :
gcc objcbind.mm -o objcbind -framework Foundation -lstdc++
가 출력
:doSomething: self=0x01a85f70 _cmd=0x00602220 result1: 15 doSomething: self=0x01a83d70 _cmd=0x00602220 result2: 43
합리적인 보이는! –
만약 내가 다른 사람이 찾고있는 경우에 boost :: framework 대신 boost :: bind 대신에 std :: bind로 위와 같은 것을 얻을 수있다. 위의 예제에 대해 아담에게 감사드립니다! –
가능한 한 부스트를 피하는 것이 가장 좋습니다. 나는 그 때 그것을 사용하여 붙어 있었다. –