从Objective C类调用C ++构造函数 [英] Call a C++ constructor from an Objective C class

查看:121
本文介绍了从Objective C类调用C ++构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从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


解决方案

当我最终在一个情况下,默认构造函数不剪切它,我使实例变量一个指针,然后使用 init中的 new 方法中方法和删除
$ b $ b

实际上,对于Objective-C实例变量,默认构造函数是相对最近调用的。



没有指定Objective- C语言,更不用说Objective-C ++扩展的规范。苹果公司发布了一份文档,名为 Objective-C编程语言,但它几乎没有提到C ++,所以你经常留在自己的时候,你需要澄清一些不明显的东西。 Clang 邮件列表通常知道更好。


How can I call a C++ constructor from inside an Objective C class?

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

解决方案

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 new in the init method and delete in the dealloc method.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆