允许C ++抽象类使用Objective-C委托 [英] Allow a C++ abstract class to use an Objective-C delegate

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

问题描述

我有一个抽象类和他的具体类实现。
我想做的是在iOS应用程序(Objective-C)中公开具体的类,并使用协议将事件分派给ViewController。



这个想法是将符合adHoc协议的客观C代表添加到这个具体的类。
任何想法如何实现?



不幸的是,我读到Objective-C ++不能继承C ++类。



提前谢谢。

解决方案

一个客观的c ++类不能继承自c ++类,包含一个

  @interface DelegateObject:NSObject 
{
}
- (id)init ;
- (void)do_something:(NSObject *)东西;
@end

struct cpp_thing
{
//这可能是一个模板,如果你愿意,或者你可以拿一个副本
//或其他你想要
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 *)东西
{
_cpp-> do_something(thing);
}

@end


I have an abstract class and his concrete class implementation. What I would like to do is to expose the concrete class inside an iOS application (Objective-C) and use protocols to dispatch the events to a ViewController.

The idea is to add an objective C delegate, that conforms an adHoc protocol, to this concrete Class. Any Idea how to achieve it?

Unfortunately I read that Objective-C++ cannot inherits from a C++ class.

Thank you in advance.

解决方案

an objective c++ class can't inherit from a c++ class but it can contain one

@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 ++抽象类使用Objective-C委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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