c++ - 从 C++ 对象调用 Objective-C 父对象

标签 c++ objective-c mixing

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

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

在伪代码中:

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

这有可能以优雅的方式实现吗?

最佳答案

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

Objective-C++ 文件,mylib.mm(注意 .mm 扩展名):

#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):

#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):

#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 

请注意,此示例仅用于说明目的,并非生产质量。

关于c++ - 从 C++ 对象调用 Objective-C 父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38157689/

相关文章:

c++ - 带锁的语句重新排序

c# - 如何在 Linux 上使用内置的 Kinect 驱动程序?

iphone - 在 ios 中直接使用 writestream 到 IP 和端口

ios - CNContactPickerViewController导航栏颜色

ios - 自定义 CIKernel 和 iOS7

java - 在 Android 上使用 Jmusic 混合音频

引用的 C++ 复制省略

c++ - sscanf()的问题不会读取char数组中的每个0

java - Swing 分层 - 透明组件忽略底层 AWT 元素

c++ - 将 C++ 类用于 Objective-C 类的推荐方法最小化对 Objective-C++ 的使用?