Since xcode 4.2, objective-C supports arc, which is a great boon for the majority of ipone developers. You don't have to face the ugly scenes like full screen [OBJ release] and [pool drain ].CodeMore importantly, you don't have to worry about the problem of releasing objects all day long. However, many developers who have switched from C ++ to objc prefer obj-C ++. Cocoa is responsible for displaying the interface, and C ++ is responsible for implementing the business logic. The combination is perfect.
The question is: can obj-C ++ perfectly support arc? Can it work normally when I put the objcc object into the STL container?
Congratulations! The answer is yes!
Test code 1:
# 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> " );
}
}
}
}
Output result:
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>
Test code 2:
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 ();
}
}
}
Output result:
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