Calling Objective-C parent from C++ object

倾然丶 夕夏残阳落幕 提交于 2019-12-11 04:29:21

问题


Creating Audio Unit Extensions on iOS requires mixing of C++ and Objective-C classes. As a result I now have an Objective-C object with a C++ object as one of its variables.

I would like the C++ child object to be able to notify its Objective-C parent/owner of certain state changes.

In pseudo-code:

void cppChildObject::callMom() {
   objectiveCParent::notificationMethod();
}

Is this possible in an elegant way?


回答1:


It depends on how you define elegant... This is possible in a fairly elegant way if the C++ class is in an Objective-C++ file (one with the .mm extension) and is not used directly by C++ code that is not in an Objective-C++ source file. The problem is that C++ code can use Objective-C types only if it is in an Objective-C++ source file. Here is a quick example, hopefully you will find it helpful.

Objective-C++ file, mylib.mm (note the .mm extension):

#import "objcpp.h"
#import <stdio.h>
#import "cpp.h"

// A C++ class in an Objective-C++ file.
// This C++ class would not need to sub-class ParentNotifier, and ParentNotifier
// would not be needed at all if it were not for OutsiderCPP, which talks to its 
// Objective-C parent through InsiderCPP.
class InsiderCPP : public ParentNotifie {
public:
    InsiderCPP(MyClassOCPP * parent) : myParent(parent){}            
    void doSomething() {
        callMom("To Mom from insider.");
    }
    void callMom(const char * msg) {
        [myParent notificationMethod:msg];
    }        
private:
    MyClassOCPP * __weak myParent;
};

@interface MyClassOCPP ()

@property InsiderCPP * insiderChild;
@property OutsiderCPP * outsiderChild;

@end

@implementation MyClassOCPP
-(id)init {
    self.insiderChild = new InsiderCPP(self);
    self.outsiderChild = new OutsiderCPP(self.insiderChild);            
    return self;
}        
-(void)doWork {
    self.insiderChild->doSomething();
    self.outsiderChild->doSomething();
}        
-(void)notificationMethod:(const char *)msg {
    printf("Parent has been notified with: %s\n", msg);
}
-(void)dealloc {
    delete self.insiderChild;
    delete self.outsiderChild;
}
@end

Here is the corresponding header, objcpp.h:

#ifndef objcpp_h
#define objcpp_h

#import <Foundation/Foundation.h>

@interface MyClassOCPP : NSObject
-(id)init;
-(void)dealloc;
-(void)doWork;
-(void)notificationMethod:(const char*)msg;
@end

#endif 

Here is a "pure" C++ source file (mylib.cpp) having nothing to do with Objective-C:

#include <stdio.h>
#include "cpp.h"

void OutsiderCPP::callMom(const char * m) {
    myParent->callMom(m);
}
void OutsiderCPP::doSomething() {
    callMom("To Mom from outsider.");
}

and here is the corresponding header (cpp.h):

#ifndef cpp_h
#define cpp_h

class ParentNotifier
{
public:
    virtual void callMom(const char *) = 0;
};

class OutsiderCPP
{
public:
    OutsiderCPP(ParentNotifier * p) : myParent(p) {}
    void doSomething();
    void callMom(const char *);

private:
    ParentNotifier * myParent;
};

#endif 

Please note that this example is for illustration purposes only and is not production quality.



来源:https://stackoverflow.com/questions/38157689/calling-objective-c-parent-from-c-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!