简体   繁体   English

C ++对象作为Objective-C ++属性

[英]C++ object as an Objective-C++ property

I want to declare a C++ property inside my Objective-C class. 我想在Objective-C类中声明一个C ++属性。

What kind of attributes should I set it to? 我应该将它设置为什么类型的属性? It seems that strong or retain will lead to an error saying that it is not an object. 似乎strongretain会导致错误,说它不是一个对象。

How could I manage its memory properly? 我怎样才能正确管理它的内存?

You are right, the property cannot be weak , strong , or retained ; 你是对的,财产不能weakstrong ,或retained ; for that it would have to be a pointer to an Objective-C object. 因为它必须是指向Objective-C对象的指针。 If you don't use any attributes on a C++ property, it will default to unsafe_unretained,assign,atomic . 如果不在C ++属性上使用任何属性,则默认为unsafe_unretained,assign,atomic

A few other things to consider, assuming the Objective-C(++) object controls the lifetime of the C++ property: 假设Objective-C(++)对象控制C ++属性的生命周期,还需要考虑其他一些事项:

  • Because you can't do much with a C++ object in Objective-C, the 因为你无法在Objective-C中使用C ++对象,所以
    property is mostly useful in Objective-C++ code, where you can mix 属性在Objective-C ++代码中非常有用,您可以在其中混合使用
    Objective-C and C++. Objective-C和C ++。
  • Because you have to manage the property's memory on your own, you need a custom setter. 因为您必须自己管理属性的内存,所以需要自定义setter。
  • Because the property defaults to atomic , you need to use synchronization in the setter and need a custom getter, too. 因为属性默认为atomic ,所以您需要在setter中使用同步并且还需要自定义getter。 You could declare it nonatomic , in which case you would not need to synchronize and would not need a custom getter. 你可以声明它nonatomic ,在这种情况下你不需要同步,也不需要自定义的getter。
  • You can implement dealloc to ensure the C++ object is freed when the Objective-C++ object goes away. 您可以实现dealloc以确保在Objective-C ++对象消失时释放C ++对象。

Here's some useful documentation from Apple: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html . 以下是Apple的一些有用文档: https//developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

And here is a quick example. 这是一个简单的例子。 Let's say the C++ class you are using for the property is called MyCPP . 假设您为该属性使用的C ++类称为MyCPP In a header you could have: 在标题中你可以:

@interface ClassOCPP : NSObject

// This property can only be used in Objective-C++ code
#ifdef __cplusplus
@property /*(unsafe_unretained,assign,atomic)*/ MyCPP * myCPP;
#endif

// Other stuff may be usable in regular Objective-C code.

@end

Implementation could be as follows (in a .mm file; remember, it's Objective-C++): 实现可以如下(在.mm文件中;记住,它是Objective-C ++):

@implementation ClassOCPP
{
    MyCPP * _myCPP;
}
-(id)init {
    self = [super init];
    _myCPP = NULL;
    return self;
}

-(void)setMyCPP:(MyCPP*)newVal {
    @synchronized(self) {
        delete _myCPP;  // it's OK to delete a NULL pointer
        _myCPP = newVal;
    }
}

-(MyCPP*)myCPP {
    return _myCPP;
}

-(void)dealloc {
    puts("De-allocating ClassOCPP.");
    delete _myCPP;
}

@end

IIRC,你需要像任何其他C ++对象一样管理它,所以只需使用assign作为属性,你应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM