Md1079
Md1079

Reputation: 1360

Calling objective C method from c++ class method

Is it possible to call an objective C method from a C++ class method? I am aware this has been answered to an extent but none of the accepted answers appear to be working for me as I get 'use of undeclared identifier' when trying to use the objective C instance variable (a pointer to self) to call the method.

@interface RTSPHandler : NSObject {   
    id thisObject;
}

implimentation:

-(int)startRTSP:(NSString *)url {

    thisObject = self;
    //  start rtsp code  
}

void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
                              struct timeval presentationTime, unsigned ) {

    [thisObject receivedRTSPFrame:fReceiveBuffer];

}    

-(void)receivedRTSPFrame:(NSMutableData* )data {

    // decode frame..

}        

error: use of undeclared identifier 'thisObject'

Upvotes: 1

Views: 1095

Answers (1)

Daniyar
Daniyar

Reputation: 3003

Try to declare thisObject as a static variable like below

static id thisObject;
@implementation RTSPHandler
//...
@end

UPDATE

Ok. Now I see my answer is hilarious. Let's get through the task and make the solution more appropriate.

There will be two separate classes with separated interface and implementation parts. Say the objective-c class named OCObjectiveClass (Objective-c class) and DummySink (C++ class). Each DummySink instance must have an object of OCObjectiveClass as a c++ class member.

This is interface part of OCObjectiveClass (with a ".h"-extension):

@interface OCObjectiveClass : NSObject
  //...
  - (void)receivedRTSPFrame:(void *)frame; // I don't know what is the frame's type and left it with a simple pointer
  //...
@end

This is the interface part of DummySink (with a ".h"-extension too):

#import "OCObjectiveClass.h" // include objective-c class headers
class DummySink
{
  OCObjectiveClass *delegate; // reference to some instance
  //...
  void AfterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,struct timeval presentationTime, unsigned);
  //...
}

AfterGettingFrame function realization must be in DummySink class implementation part (not ".cpp" extension, it must be ".mm" to work with objective-c classes and methods).

void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
                              struct timeval presentationTime, unsigned ) {

    [delegate receivedRTSPFrame:fReceiveBuffer];

} 

Don't forget to set the delegate value.

- (void)someMethod
{
  OCObjectiveClass *thisObject;
  // initialize this object
  DummySink sink;
  sink.delegate=thisObject;
  sink.DoWork();
}

Upvotes: 1

Related Questions