2009-06-19 4 views

答えて

15

次の2つの非表示パラメータ、それぞれselfとタイプid_cmdSELと単なるC関数であるメッセージ実装(IMP)に結合することができなければなりません。

EDIT:次の完全な例をテストしただけで動作するようです。 GNUstepので

#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; 
} 

、としてコンパイル:Mac OS Xでは

gcc objcbind.mm -o objcbind -I/usr/include/GNUstep -lobjc -lstdc++ -lgnustep-base 

、としてコンパイル:出力

gcc objcbind.mm -o objcbind -framework Foundation -lstdc++ 

 
doSomething: self=0x01a85f70 _cmd=0x00602220 
result1: 15 
doSomething: self=0x01a83d70 _cmd=0x00602220 
result2: 43 
+0

は、合理的なルックス! –

+1

他の誰かが私のように見ている場合は、boost :: bindの代わりにstd :: bindを使って上記を達成することもできます。上の例のアダムに感謝します! –

+0

可能であれば、ブーストを避けるのが最善です。私は当時それを使用して立ち往生した。 –

関連する問題