博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实验证明:Objective-C++ 完美支持 ARC
阅读量:6809 次
发布时间:2019-06-26

本文共 4759 字,大约阅读时间需要 15 分钟。

  从 XCode 4.2 开始 Objective-C 支持 ARC,对于广大 iPone 开发者来说是巨大的福音,不用面对满屏 [obj release] 和 [pool drain] 这类丑陋不堪的代码了,更重要的是不用整天为对象释放问题搞得寝食难安。但对于许多从 C++ 转到 ObjC 的开发者来说,其实更喜欢 Obj-C++ 混编。Cocoa 负责界面展现,C++ 负责业务逻辑实现,组合起来十分完美。

  问题是 Obj-C++ 能否完美支持 ARC 呢,特别是把 ObjcC 对象放入 STL 容器的情形下能否正常工作?

  恭喜大家,答案是肯定的!

 

测试代码一:

#import 
#import
using namespace std; class Y {
public: int value; public: Y(int val = 0) : value(val) {
NSLog(@"Y.Y (%d)", value); } ~Y() {
NSLog(@"Y.~Y (%d)", value); } }; @interface T : NSObject {
@private int value; Y* y; } - (id) initWithValue:(int)v; @end @implementation T - (id) init {
return [self initWithValue:0]; } - (id) initWithValue:(int)v {
self = [super init]; self->value = v; self->y = new Y(v); NSLog(@"T.init (%d)", self->value); return self; } - (void)dealloc {
delete self->y; NSLog(@"T.dealloc (%d)", self->value); } @end class X {
public: int value; T* t; public: X(int val = 0) : value(val) {
NSLog(@"X.X (%d)", value); } ~X() {
NSLog(@"X.~X (%d)", value); } };
int main(int argc, char *argv[]) {
@autoreleasepool {
T* t = [[T alloc] initWithValue:1]; {
vector
vt; vt.push_back(t); t = nil; vt.push_back([[T alloc] initWithValue:2]); vt.push_back([[T alloc] initWithValue:3]); {
//vt.pop_back(); vt.erase(vt.begin()); NSLog(@"< first element had been dealloc >"); } {
vt.clear(); NSLog(@"< all other elements had been dealloc >"); } } } }

输出结果:

2012-02-11 19:31:56.256 Switcher[3037:f803] Y.Y        (1) 2012-02-11 19:31:56.258 Switcher[3037:f803] T.init        (1) 2012-02-11 19:31:57.352 Switcher[3037:f803] Y.Y        (2) 2012-02-11 19:31:57.353 Switcher[3037:f803] T.init        (2) 2012-02-11 19:31:57.578 Switcher[3037:f803] Y.Y        (3) 2012-02-11 19:31:57.579 Switcher[3037:f803] T.init        (3) 2012-02-11 19:31:57.786 Switcher[3037:f803] Y.~Y        (1) 2012-02-11 19:31:57.787 Switcher[3037:f803] T.dealloc    (1) 2012-02-11 19:31:58.894 Switcher[3037:f803] < first element had been dealloc > 2012-02-11 19:32:05.227 Switcher[3037:f803] Y.~Y        (2) 2012-02-11 19:32:05.228 Switcher[3037:f803] T.dealloc    (2) 2012-02-11 19:32:05.229 Switcher[3037:f803] Y.~Y        (3) 2012-02-11 19:32:05.230 Switcher[3037:f803] T.dealloc    (3) 2012-02-11 19:32:07.787 Switcher[3037:f803] < other elements had been dealloc >

 

测试代码二:

int main(int argc, char *argv[]) {
@autoreleasepool {
X* x = new X(1); x->t = [[T alloc] initWithValue:1]; {
list
vx; vx.push_back(x); vx.push_back(new X(2, [[T alloc] initWithValue:2])); vx.push_back(new X(3, [[T alloc] initWithValue:3])); {
X* x2 = vx.front(); vx.pop_front(); delete x2; NSLog(@"< first element had been dealloc >"); } {
for(list
::iterator it = vx.begin(); it != vx.end(); ++it) delete (*it); NSLog(@"< all other elements had been dealloc >"); } vx.clear(); } } }

 

输出结果:

2012-02-12 02:34:13.403 Switcher[3142:f803] X.X        (1) 2012-02-12 02:34:14.516 Switcher[3142:f803] Y.Y        (1) 2012-02-12 02:34:14.517 Switcher[3142:f803] T.init        (1) 2012-02-12 02:34:26.171 Switcher[3142:f803] Y.Y        (2) 2012-02-12 02:34:26.171 Switcher[3142:f803] T.init        (2) 2012-02-12 02:34:26.174 Switcher[3142:f803] X.X        (2) 2012-02-12 02:34:42.481 Switcher[3142:f803] Y.Y        (3) 2012-02-12 02:34:42.481 Switcher[3142:f803] T.init        (3) 2012-02-12 02:34:42.484 Switcher[3142:f803] X.X        (3) 2012-02-12 02:34:48.856 Switcher[3142:f803] X.~X        (1) 2012-02-12 02:34:48.857 Switcher[3142:f803] Y.~Y        (1) 2012-02-12 02:34:48.858 Switcher[3142:f803] T.dealloc    (1) 2012-02-12 02:35:02.491 Switcher[3142:f803] < first element had been dealloc > 2012-02-12 02:35:07.303 Switcher[3142:f803] X.~X        (2) 2012-02-12 02:35:07.303 Switcher[3142:f803] Y.~Y        (2) 2012-02-12 02:35:07.304 Switcher[3142:f803] T.dealloc    (2) 2012-02-12 02:35:09.319 Switcher[3142:f803] X.~X        (3) 2012-02-12 02:35:09.320 Switcher[3142:f803] Y.~Y        (3) 2012-02-12 02:35:09.321 Switcher[3142:f803] T.dealloc    (3) 2012-02-12 02:35:11.268 Switcher[3142:f803] < all other elements had been dealloc >

转载地址:http://mnwzl.baihongyu.com/

你可能感兴趣的文章
工作总结的字体和格式要求
查看>>
CentOS 6.9永久设置静态路由表以及路由表常用设置
查看>>
解决Docker时区与主机时区不一致的问题
查看>>
思考与知识
查看>>
访问日志不记录静态文件 访问日志切割 静态元素过期时间
查看>>
idea中复制module和module中的蓝色tag出现的方法
查看>>
python中的面相对象
查看>>
Spring缓存注解@Cache使用
查看>>
去除wordpress的category各方法对比
查看>>
traceroute
查看>>
精通汇编语言,有兴趣一起搞破解的请进!
查看>>
一步一步写一个简单通用的makefile(三)
查看>>
asp and javascript: sql server export data to csv and to xls
查看>>
一起谈.NET技术,.NET框架:为什么我们要尽量使用框架内建的功能,而不是重新发明...
查看>>
云计算中我们是否需要LAMP的PaaS?
查看>>
研究称Android内核存在漏洞 黑客可窃取电邮
查看>>
C#缺省参数可以让代码变得更加简洁明了与时俱进心里敞亮了很多了
查看>>
【自然框架】js版的QuickPager分页控件 V2.0
查看>>
poj-2049 Finding Nemo *
查看>>
模块化编程本质探讨
查看>>