c++ - ARC 和 C++ 对象内的 Objective C 对象会发生什么?

标签 c++ objective-c automatic-ref-counting

我很惊讶这个编译没有错误。当 ARC 刚推出时,我不认为以前会这样。

class SessionWriter
{
    NSString *sessionDir;
    ...
};

看起来NSString是由ARC在析构函数中释放的。这是在哪里记录的?

最佳答案

Objective-C++ 的文档总体来说相当缺乏。除了 Apple 的高级教程和低级 API 列表之外,Objective-C 2.0 语言本身缺乏正式规范,而 Objective-C++ 则更少。从广义上讲,您似乎应该相信在作为基本语言的 C 和 C++ 之间的过渡中,事情会按逻辑“工作”。

Clang ARC 文档 touch on this a bit ,尽管它们实际上描述的是 Objective-C 和 Objective-C++ 结构而不是 C++ 类之间的行为差​​异。不过,这非常清楚地表明,ARC 确实适用于 Objective-C++ 聚合。

nontrivally ownership-qualified types are considered non-POD: in C++11 terms, they are not trivially default constructible, copy constructible, move constructible, copy assignable, move assignable, or destructible. It is a violation of C++’s One Definition Rule to use a class outside of ARC that, under ARC, would have a nontrivially ownership-qualified member.

Rationale

Unlike in C, we can express all the necessary ARC semantics for ownership-qualified subobjects as suboperations of the (default) special member functions for the class. These functions then become non-trivial. This has the non-obvious result that the class will have a non-trivial copy constructor and non-trivial destructor; if this would not normally be true outside of ARC, objects of the type will be passed and returned in an ABI-incompatible manner.

关于c++ - ARC 和 C++ 对象内的 Objective C 对象会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439351/

相关文章:

c++ - 有没有一种简单的方法可以确定 boost::adjacency_list 是否为空?

ios - "UIPopoverPresentationController presentationTransitionWillBegin"使用 UIPopoverController 时崩溃

ios - GCDAsyncSocket 委托(delegate)调用不起作用

ios - 要在 ARC 下的自定义 UIView 的 dealloc 中消除导出?

iphone - ARC - UIWebView/UIViewController 留在内存中吗?

c++ - 隐式声明的默认构造函数采用什么形式?

c++ - 在 Stack 实现中使用哪个智能指针?

c++ - 使用 void 指针时转换函数(CUBA Nint 例程)

ios - 添加到购物车功能 - 核心数据

ios - for循环后,未知引号添加到NSMutableArray中的字符串对象