允许C++抽象类使用 Objective-C 委托
Allow a C++ abstract class to use an Objective-C delegate
我有一个抽象类和他的具体类实现。我想做的是在iOS应用程序(Objective-C)中公开具体类,并使用协议将事件调度到ViewController。
这个想法是添加一个符合 adHoc 协议的目标 C 委托到这个具体的类中。知道如何实现它吗?
不幸的是,我读到Objective-C++不能从C++类继承。
提前谢谢你。
目标 C++ 类不能从 C++ 类继承,但它可以包含一个
@interface DelegateObject : NSObject
{
}
- (id) init;
- (void) do_something: (NSObject*) thing;
@end
struct cpp_thing
{
// this could be a template if you wished, or you could take a copy
// or whatever you want
void do_something(NSObject* thing) { };
};
@implementation DelegateObject
{
std::shared_ptr<cpp_thing> _cpp;
}
- (id) init
{
if (self = [super init])
{
_cpp = std::make_shared<cpp_thing>();
}
return self;
}
- (void) do_something: (NSObject*) thing
{
_cpp->do_something(thing);
}
@end
尝试建立关系。 目标C++对象要么有C++对象,反之亦然。 然后让它们彼此足够了解,以便目标对象C++对象将消息中继到C++对象。
如果你声明了一个指向id的弱指针,你可以声明一个抽象委托(NSObject中的一个类别),然后在CPP源中自由调用方法。 只需导入包含抽象委托的文件,然后在 ObjC 类中实现方法本身,即可获得委托,而无需声明实际委托。
相关文章:
- 如何委托派生类使用其父构造函数?
- 如何将数组从Objective-C发送到C++函数
- 如何在 C++ 中获取中继器的委托数据?
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- C++ DLL(不是 CLI)是否可以调用 .NET Core 3.0 委托?
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字
- Mac Snow Leopard 上的 Qt 5.3.2:ld:在架构x86_64 [clang、Objective-C
- 将方法委托给成员的重写运算符>
- 创建跨平台 C++ 触摸管理器.在 c++ 中传递 Objective-c 对象涉及代码
- 构造函数委托与默认参数
- C++对象作为 Objective-C++ 属性
- 从未调用过核心蓝牙委托方法(目标 c++)
- 如何在不使用委托的 QML 中显示模型数据
- 如何访问委托的paint()函数中的另一个索引?
- 是否可以使C++类成为Objc类的委托
- 在 Objective-C 和 Swift 之间委托问题
- 使用委托在C++类和 Objective-C 类之间进行通信
- 允许C++抽象类使用 Objective-C 委托
- 通过委托方法让C++模型通知Objective-C++控制器
- 通过c++抽象层实现ARC Objective-C委托