我很惊讶这个编译没有错误。当 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/