C++ 中的 Objective C 成员班级

发布于 2024-09-28 11:14:47 字数 389 浏览 2 评论 0原文

是否有可能在 c++ 类中拥有一个客观的 c 成员

@interface ObjectiveCClass : UIViewController  {

    int someVarialbe;

}
- (void)someFunction;

@end


class CPlusPlusClass{
      ObjectiveCClass obj;          // have a objective c member

      void doSomething(){
           obj.someFunction;        // and call a objective c method
       }
};

任何指导都将非常感激。

干杯

Is it possible to have a objective c member in a c++ class

@interface ObjectiveCClass : UIViewController  {

    int someVarialbe;

}
- (void)someFunction;

@end


class CPlusPlusClass{
      ObjectiveCClass obj;          // have a objective c member

      void doSomething(){
           obj.someFunction;        // and call a objective c method
       }
};

Any guidance would really be appreciated.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2024-10-05 11:14:47

要创建可以在 obj-c 和 cpp 代码之间共享的头文件,您可以使用编译器预定义的宏来执行以下操作:

// A .h file defining a objc class and a paired cpp class
// The implementation for both the objective C class and CPP class
// MUST be in a paired .mm file
#pragma once

#ifdef __OBJC__
#import <CoreFoundation/CoreFoundation.h>
#else
#include <objc/objc.h>
#endif

#ifdef __OBJC__

@interface ObjectiveCClass :
...

typedef ObjectiveCClass* ObjectiveCClassRef;

#else

typedef id ObjectiveCClassRef;

#endif

#ifdef __cplusplus

class CPlusPlusClass {
  ObjectiveCClassRef obj;

  void doSomethind();
};

#endif

我不是 100% 确定让 ObjectiveCClassRef 更改类型(如 c/cpp 和 obj-c 之间的类型)是合法的构建。
但是 id 是目标 C 头文件中定义的 ac/cpp 兼容类型,能够存储目标 C 类指针,并且在 .m 或 .mm 文件中使用时,允许您调用该对象直接使用 Objective C 语法。

To create header files that can be shared between obj-c and cpp code, you could use the compiler predefined macros to do something like:

// A .h file defining a objc class and a paired cpp class
// The implementation for both the objective C class and CPP class
// MUST be in a paired .mm file
#pragma once

#ifdef __OBJC__
#import <CoreFoundation/CoreFoundation.h>
#else
#include <objc/objc.h>
#endif

#ifdef __OBJC__

@interface ObjectiveCClass :
...

typedef ObjectiveCClass* ObjectiveCClassRef;

#else

typedef id ObjectiveCClassRef;

#endif

#ifdef __cplusplus

class CPlusPlusClass {
  ObjectiveCClassRef obj;

  void doSomethind();
};

#endif

Im not 100% sure its legal to have ObjectiveCClassRef change type like that between c/cpp and obj-c builds.
But id is a c/cpp compatible type defined in the objective C header files as capable of storing an objective C class pointer, and, when used in .m or .mm files, allows you to call the object directly using objective C syntax.

我家小可爱 2024-10-05 11:14:47

Objective-C 有一种方言,称为 Objective-C++,它可以与 C++ 互操作,就像 Objective-C 与 C 互操作一样。您可以将文件的设置更改为 Objective-C++,或者将扩展名更改为“.mm” ”。当然,您仍然需要通过指针访问 Objective-C 对象并执行 alloc-init 之类的操作。

There's a dialect of Objective-C called Objective-C++ that is interoperable with C++ the same way that Objective-C is interoperable with C. You can either change the setting for the file to be Objective-C++ or change the extension to ".mm". You'll still need to access Objective-C objects through pointers and do the alloc-init dance and all that, of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文