在Objective-C ++的.h文件中添加指向已定义的Objective-C类的指针 [英] Add pointer to defined Objective-C class in .h file of Objective-C++

查看:94
本文介绍了在Objective-C ++的.h文件中添加指向已定义的Objective-C类的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Objective-C ++中创建一个类,该类的字段是指向Objective-C接口的指针(在.h文件内部)?

Is possible to create class in Objective-C++ which has field that is a pointer to an Objective-C interface ( inside the .h file ) ?

为了澄清一点,我创建了一个Objective-C界面:

To clarify a little, I created one Objective-C interface:

// Our platform independent class
@interface LoggerDelegate : NSObject<SomeDelegate>
@end

我可以在.mm文件中创建它,并且可以工作,但是它是为全局变量而设计的,不允许更改它的范围.所以我想在Logger.h:

I can create it inside .mm file and it works but it is meant for a global variable and I am not allowed to change its scope. So I would like to use it inside Logger.h:

class Logger {
public:

private:

LoggerDelegate* _ptr;   //<--- pointer to the Objective-C interface
};

当我在Logger.mm中定义我的界面时,它可以工作.但是是否可以在.h文件中合并来自Objective-C和C ++的Objective-C ++代码?还是只能在.mm文件中使用?

When I define my interface inside Logger.mm it works. But is it possible to merge in Objective-C++ code from Objective-C and C++ inside .h files ? Or is it only possible in .mm files ?

推荐答案

语言族

Objective-C 使用.h标头和.m实现文件.如果是抽象接口,则可能不需要后者.所有这些文件都必须符合Objective-C语法:

The language family

Objective-C uses .h headers and .m implementation files. You may not need the latter in case of an abstract interface. All these files have to comply with the Objective-C syntax:

  • 您可以在.h.m中使用某些C语法;它将被认可,因为目标C是C的语言扩展.
  • 纯C ++语言元素(例如classtemplate)将无法被Objective-C编译器识别.
  • you may use some C syntax in the .h or .m; it will be recognized, because Objective C is a language extension of C.
  • pure C++ language elements (e.g. class or template) will not be recognized by the Objective-C compiler.

Objective-C ++ 也使用.h标头,但使用.mm实现文件.两者都必须符合Objective-C ++语法:

Objective-C++ uses also .h headers but .mm implementation files. Both have to comply with the Objective-C++ syntax:

  • 大多数C ++语法将被识别,因为Objective C ++基于C ++.
  • 因为Objective C是Objective-C ++的子集,所以将识别所有Objective-C语法.

标准C ++ 使用标头(通常.h.hpp)和实现文件(通常为.cc.cpp).这些必须仅使用C ++语言元素.不属于C ++的Objective-C和Objective-C ++语言元素,例如@interface,将触发C ++编译错误.

Standard C++ uses headers (usually .h or .hpp) and implementation files (usually .cc or .cpp). These must use only C++ language elements. Objective-C and Objective-C++ language elements that do not belong to C++, such as for example @interface will trigger C++ compilation errors.

混合Objective-C和Objective-C ++::如果您在Objective-C和Objective C ++之间共享标头(.h),则它们必须同时符合Objective-C和Objective-C C ++.因此,标头只能使用两种语言之间的公共分母,即Objective-C(和C)语言元素.

Mixing Objective-C and Objective-C++: If you share the headers (.h) between Objective-C and Objective C++, they will have to comply with both Objective-C and Objective-C++. In consequence, the header can use only the common denominator between the two languages, that is Objective-C (and C) language elements.

混合标准C ++和目标C/C ++ :您只能在Objective-C ++中将C ++语言与Objective-C或Objective-C ++构造结合在一起(所以.mm文件,最后是.h当且仅当未包含在任何标准C ++或Objective-C源代码中)

Mixing standard C++ and objective C/C++: You can combine C++ language with Objective-C or Objective-C++ constructs only in Objective-C++ (so .mm files, and eventually .h if and only if not included in any standard C++ or Objective-C source code)

这就是为什么您可以在.mm文件中使用Objective-C界面的原因.但是您不能在C ++代码中包含的.h文件中使用它.

THis is why you may use your Objective-C interface in a .mm file. But you can't use it in a .h file that would be included in C++ code.

标准C ++和Objective-C/C ++的对象模型和生命周期不同.这使得互操作性变得困难.例如,C ++类不能从Objective-C类继承,并且如果Objective-C类包含C ++对象,则

The object model and lifecycle of standard C++ and the Objective-C/C++ are not the same. This makes interoperability difficult. For example a C++ class can't inherit from an Objective-C class, and if an Objective-C class contains a C++ object the construction can be tricky. And there is no C++ language construct that would be guaranteed to be natively compatible with the Objective-C++ @interface either.

使用 PIMPL,可以管理从C ++到Objective-C ++对象的指针习惯用语(也称为不透明指针"). 这篇来自Phil Jordan的精彩文章解释了如何做这个例子很容易重用.

It is possible to manage pointers from C++ to Objective-C++ objects by using the PIMPL idiom (also called "the opaque pointer"). This brilliant article from Phil Jordan explains how to do this with an example that you could easily reuse.

Phil Jordan的主要思想是使用标头,使用指向不完整对象的指针将Objective-C ++对象包装到标准C ++类中.菲尔(Phil)以巧妙的方式使用条件编译,以允许将相同的.h用于跨语言用途:

Phil Jordan's main idea is to use a header to wrap the Objective-C++ object into a standard C++ class using a pointer to an incomplete object. Phil uses conditional compilation in a clever way to allow the use of the same .h for cross-language purpose:

#ifdef __OBJC__
@class XXXX;       // if header used in Objective C++
#else
struct XXXX;        // if header used in standard C++
#endif

class WrapperClass {   // standard C++ class also recognuzed by Objective C++
    XXXX *wrapped_objective_c_object_pointer; 
public:  
    ...// public interface for C++
};

这确保了包含标头的C ++代码永远不能使用指针对包装的对象执行任何操作.然后,将在.mm文件中提供C ++包装器类的实现,该文件了解Objective C ++的语义.我强烈建议您阅读本文以了解该解决方案的所有详细信息.

This ensures that C++ code that includes the header can never use the pointer to perform any operation on the wrapped object. The implementation of the C++ wrapper class will then be provided in a .mm file, which is aware of the Objective C++ semantic. I strongly recommend that you read the article to understand all the details of that solution.

这篇关于在Objective-C ++的.h文件中添加指向已定义的Objective-C类的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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