我正在大量使用Objective-C ++。我知道虽然我可以在Objective-C代码中包含C ++代码,但反之亦然。我想通过分布式对象将我创建的自定义C ++对象提供给Objective-C进程。当我尝试这样做时,我收到以下错误消息:
error: cannot convert ‘custom_class*’ to ‘objc_object*’ in argument passing
有没有办法将自定义C ++对象转发给另一个Objective-C进程?
我的代码:
#import <Cocoa/Cocoa.h>
#import <iostream>
#import <stdio.h>
using namespace std;
class custom_class {
public:
custom_class();
~custom_class();
void hello();
};
custom_class::custom_class() { }
custom_class::~custom_class() { }
void custom_class::hello() { cout << "Hello World!" << endl; }
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
cout << "Starting vendor " << endl;
custom_class *test = new custom_class();
NSConnection *connection = [NSConnection connectionWithReceivePort:[NSPort port] sendPort:nil];
[connection setRequestTimeout:1];
[connection setRootObject:test];
[connection registerName:@"DisObjTest"];
[[NSRunLoop currentRunLoop] run];
[pool drain];
return 0;
}
答案 0 :(得分:4)
如果不在Objective-C类中包装C ++类,就无法做到这一点。所有使分布式对象成为可能的运行时机制在C ++中都不可用。
答案 1 :(得分:-1)
将其包裹在Objective-C界面中:
class custom_class;
@interface custom_class_wrapper : NSObject
@propery (nonatomic, assign) custom_class* obj;
@end
或者在NSValue
内,这需要演员
custom_class a;
id obj = [NSValue valueWithPointer:&a];
custom_class* a_val = static_cast<custom_class*>([obj pointerValue]);