首先也是最基本的将.m文件改为.mm后缀通知complier使用混合编译模式兼容c++代码.
其次objective-c的构造函数格式与c++不同, 需要为c++的对象写一个wrapper.
例如
.h:
@interface objectiveCclass:NSObject
Cppclass *cppclass;
@end.mm:
@implementation objectiveCclass
@end
-(id) init{
if(self = [super init]) {
cppclass = new Cppclass();
}
return self;
}
}
同样的由于传参方式的不同需要给很多函数写一个objective-c外壳. 同时注意由于在objective-c使用的是object指针的缘故需要用cppclass->cppmethod()的格式调用函数.
再次由于objective-c只使用指针, 对于某些接收object为变量的c++函数需要重写.
最后objective-c不支持virtual函数, 该重写的也重写吧.
总而言之, c++在objective-c下使用受限, 但并非完全不可使用.