从C ++对象调用Objective-C父对象 [英] Calling Objective-C parent from C++ object

查看:100
本文介绍了从C ++对象调用Objective-C父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS上创建音频单元扩展需要混合C ++和Objective-C类.结果,我现在有了一个带有C ++对象作为其变量之一的Objective-C对象.

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.

我希望C ++子对象能够将某些状态更改通知其Objective-C父/所有者.

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

使用伪代码:

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

这是否可以优雅地做到?

Is this possible in an elegant way?

推荐答案

这取决于您如何定义优雅...如果C ++类位于Objective-C ++文件中(一个带有.mm扩展名),并且不在Objective-C ++源文件中的C ++代码直接使用.问题是C ++代码只有在Objective-C ++源文件中才可以使用Objective-C类型.这是一个简单的示例,希望对您有所帮助.

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 ++文件,mylib.mm(请注意.mm扩展名):

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

以下是相应的标题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 

这是一个与Objective-C无关的纯" C ++源文件(mylib.cpp):

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.");
}

,这是对应的标题(cpp.h):

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.

这篇关于从C ++对象调用Objective-C父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆