實驗證明:Objective-C++ 完美支援 ARC

來源:互聯網
上載者:User

  從 XCode 4.2 開始 Objective-C 支援 ARC,對於廣大 iPone 開發人員來說是巨大的福音,不用面對滿屏 [obj release] 和 [pool drain] 這類醜陋不堪的代碼了,更重要的是不用整天為對象釋放問題搞得寢食難安。但對於許多從 C++ 轉到 ObjC 的開發人員來說,其實更喜歡 Obj-C++ 混編。Cocoa 負責介面展現,C++ 負責商務邏輯實現,組合起來十分完美。

  問題是 Obj-C++ 能否完美支援 ARC 呢,特別是把 ObjcC 對象放入 STL 容器的情形下能否正常工作?

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

 

測試代碼一:

#import <vector>
#import <list>
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<T*> 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<X*> 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<X*>::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 >

CodeProject

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.