Is possible to create class in Objective-C++ which has field that is a pointer to an Objective-C interface ( inside the .h
file ) ?
To clarify a little, I c
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
or .m
; it will be recognized, because Objective C is a language extension of C.class
or template
) will not be recognized by the Objective-C compiler.Objective-C++ uses also .h
headers but .mm
implementation files. Both have to comply with the Objective-C++ syntax:
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.
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.
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)
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.
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.
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'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++
};
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.