C lens in C ++ class

Is it possible to have an object member c 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 be truly appreciated.

Greetings

+4
source share
2 answers

To create header files that can be shared between obj-c and cpp code, you can use predefined compiler 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 that its legal to have an ObjectiveCClassRef change type similar to the one that exists between the c / cpp and obj-c lines. But id is an ac / cpp compatible type defined in the C header object files as capable of storing the C class object pointer, and when used in .m or .mm files, it allows you to directly call an object using the target C syntax.

+6
source

There, the dialect of Objective-C is called Objective-C ++, which is C ++ compatible just as Objective-C is compatible with C. You can either change the setting for the Objective-C ++ file, or change the extension to ".mm" . You still have to access Objective-C objects through pointers and perform the alloc-init dance, and all that, of course.

+2
source

All Articles