开发者

How to pass pointer to a reference as an argument to a function-Objective C

开发者 https://www.devze.com 2023-03-01 21:23 出处:网络
In C++ we will pass the pointer to a function as bool getData(REMOTE_ID msId,RsEvent*& pEvent); How to declare this in Objective C?

In C++ we will pass the pointer to a function as

bool getData(REMOTE_ID msId,RsEvent*& pEvent);

How to declare this in Objective C?

// ?
-(BOOL)getData:(REMOTE_ID)msId withEvent:(RsEvent*)开发者_JAVA技巧pEvent;

But i need to use the pointer to reference (RsEvent*&). I have used only pointer (RsEvent*) as a datatype here.


You need to use pointers to simulate reference in C (and Objective-C):

-(BOOL)getData:(REMOTE_ID)msId withEvent:(RsEvent**)pEvent {
   // do stuff with msId and *pEvent
}

and pass in the argument as a pointer:

RsEvent* event;
BOOl res = [foo getData:msId withEvent:&event];
...


(By the way, you're not dealing with a pointer to a reference, but a reference to a pointer.) Can't you just use the same type annotation in the Objective-C method?

- (BOOL)getData:(REMOTE_ID)msId withEvent:(RsEvent*&)pEvent;

I think that should work, but if it doesn't, you can use a pointer to a pointer instead:

- (BOOL)getData:(REMOTE_ID)msId withEvent:(RsEvent**)pEvent {
  useEvent(*pEvent); // when using the event, you need to dereference the pointer
}

Send the -getData:withEvent: message with the address of your event:

[obj getData:SOME_ID withEvent:&event];
0

精彩评论

暂无评论...
验证码 换一张
取 消