I have a .cpp/.hpp file combo -> the .hpp file has #include ..
I also have a .mm/.h file combo -> if I include the .hpp file within my .mm Objective C++ file, there are no issues. However, if I try to include the .hpp file within my .h (Objective C header) file, I get a preprocessor issue 'iostream not found'.
Is there any way around this other than doing funky stuff like having a void* in my Objective C .h file and then casting it as the type that is included in the .mm or wrapping every C++ type within an Objective C++ type?
My question is basically the same as Tony's question (but nobody answered his):
https://stackoverflow.com/questions/10163322/how-to-include-c-header-file-in-objective-c-header-file
The problem is that you have to avoid all C++ semantics in the header to allow normal Objective-C classes to include it. This can be accomplished using opaque pointers.
class CPPClass
{
public:
int getNumber()
{
return 10;
}
};
//Forward declare struct
struct CPPMembers;
@interface ObjCPP : NSObject
{
//opaque pointer to store cpp members
struct CPPMembers *_cppMembers;
}
@end
#import "ObjCPP.h"
#import "CPPClass.h"
struct CPPMembers {
CPPClass member1;
};
@implementation ObjCPP
- (id)init
{
self = [super init];
if (self) {
//Allocate storage for members
_cppMembers = new CPPMembers;
//usage
NSLog(@"%d", _cppMembers->member1.getNumber());
}
return self;
}
- (void)dealloc
{
//Free members even if ARC.
delete _cppMembers;
//If not ARC uncomment the following line
//[super dealloc];
}
@end