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 created one Objective-C interface:
// Our platform independent class
@interface LoggerDelegate : NSObject<SomeDelegate>
@end
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
};
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 ?
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:
- 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
ortemplate
) 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:
- Most of the C++ syntax will be recognized, because Objective C++ is based on C++.
- All the Objective-C syntax will be recognized because Objective C is a subset of Objective-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.
Mixing the languages - syntax problems
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.
Mixing the languages - sematic problems
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.
But isn't there a way to interoperate with pointers ?
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.
来源:https://stackoverflow.com/questions/46388787/add-pointer-to-defined-objective-c-class-in-h-file-of-objective-c