@protocol IntegerFun -(NSInteger)call; @end NSInteger A (NSInteger kParam, id x1, id x2, id x3, id x4, id x5); @interface B_Class : NSObject { NSInteger *k; id x1, x2, x3, x4; } -(id)initWithK:(NSInteger *)k x1:(id)x1 x2:(id)x2 x3:(id)x3 x4:(id)x4; @end @implementation B_Class -(id)initWithK:(NSInteger *)_k x1:(id)_x1 x2:(id)_x2 x3:(id)_x3 x4:(id)_x4 { if ((self = [super init])) { k = _k; x1 = [_x1 retain]; x2 = [_x2 retain]; x3 = [_x3 retain]; x4 = [_x4 retain]; } return self; } -(void)dealloc { [x1 release]; [x2 release]; [x3 release]; [x4 release]; [super dealloc]; } -(NSInteger)call { return A(--*k, self, x1, x2, x3, x4); } @end NSInteger A (NSInteger k, id x1, id x2, id x3, id x4, id x5) { id B = [[[B_Class alloc] initWithK:&k x1:x1 x2:x2 x3:x3 x4:x4] autorelease]; return k <= 0 ? [x4 call] + [x5 call] : [B call]; } @interface K : NSObject { NSInteger n; } -(id)initWithN:(NSInteger)n; @end @implementation K -(id)initWithN:(NSInteger)_n { if ((self = [super init])) { n = _n; } return self; } -(NSInteger)call { return n; } @end int main(int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSInteger result = A(10, [[[K alloc] initWithN:1] autorelease], [[[K alloc] initWithN:-1] autorelease], [[[K alloc] initWithN:-1] autorelease], [[[K alloc] initWithN:1] autorelease], [[[K alloc] initWithN:0] autorelease]); NSLog(@"%ld\n", result); [pool release]; return 0; }