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

前端 未结 2 1959
北海茫月
北海茫月 2021-01-24 13:17

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?

<
相关标签:
2条回答
  • 2021-01-24 13:37

    (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 讨论(0)
  • 2021-01-24 13:41

    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];
    ...
    
    0 讨论(0)
提交回复
热议问题