从Objective C类调用C ++构造函数 [英] Call a C++ constructor from an Objective C class
问题描述
如何从Objective C类中调用C ++构造函数?
class CppClass {
public:
CppClass(int arg1,const std :: string& arg2):_arg1(arg1),_arg2(arg2){}
// ...
private:
int _arg1; std :: string _arg2;
};
@interface ObjC:NSObject {
CppClass _cppClass;
}
@end
@implementation ObjC
- (id)init
{
self = [super init] ;
if(self)
{
//在_cppClass上调用CppClass :: CppClass(5,hello)的语法是什么?
}
return self;
}
@end
当我最终在一个情况下,默认构造函数不剪切它,我使实例变量一个指针,然后使用 实际上,对于Objective-C实例变量,默认构造函数是相对最近调用的。 没有指定Objective- C语言,更不用说Objective-C ++扩展的规范。苹果公司发布了一份文档,名为 Objective-C编程语言,但它几乎没有提到C ++,所以你经常留在自己的时候,你需要澄清一些不明显的东西。 Clang 邮件列表通常知道更好。 How can I call a C++ constructor from inside an Objective C class?
When I end up in a situation where the default constructor doesn't cut it, I make the instance variable a pointer and then use It's a relatively recent thing that default constructors are called at all for Objective-C instance variables, actually. There is no specification of the Objective-C language, let alone a specification of the Objective-C++ extension. Apple does publish a document called The Objective-C Programming Language, but it barely mentions C++, so you're often left on your own when you need to clarify something unobvious. The guys at the Clang mailing list often know better, though. 这篇关于从Objective C类调用C ++构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! init中的
方法和 new
方法中删除
。
$ b $ b
class CppClass {
public:
CppClass(int arg1, const std::string& arg2): _arg1(arg1), _arg2(arg2) { }
// ...
private:
int _arg1; std::string _arg2;
};
@interface ObjC: NSObject {
CppClass _cppClass;
}
@end
@implementation ObjC
- (id)init
{
self = [super init];
if ( self )
{
// what is the syntax to call CppClass::CppClass(5, "hello") on _cppClass?
}
return self;
}
@end
new
in the init
method and delete
in the dealloc
method.