我有个问题:
我没有明确地“ new
”和“ delete
”我的 C++ 实例,但是当 Objective-C 类初始化时它会自动调用“ new
”和“ ”。delete
任何想法?
class myCppTestClass
{
public:
myCppTestClass()
{
NSLog(@"MyCpp constructor");
}
~myCppTestClass()
{
NSLog(@"MyCpp destructor");
}
};
@interface MyTestClass : NSObject
{
myCppTestClass myCppInstance;
}
@end
@implementation MyTestClass
@end
我这样称呼它:
NSLog(@"Create an object.");
MyTestClass *objcObject = [[MyTestClass alloc] init];
NSLog(@"Object created");
[objcObject release];
objcObject = nil;
NSLog(@"Object released.");
然后我运行它,日志是这样的:
2012-11-16 12:01:18.747 iOSVersion[87248:f803] Create an object.
2012-11-16 12:01:18.749 iOSVersion[87248:f803] MyCpp constructor
2012-11-16 12:01:18.750 iOSVersion[87248:f803] Object created
2012-11-16 12:01:18.751 iOSVersion[87248:f803] MyCpp destructor
2012-11-16 12:01:18.752 iOSVersion[87248:f803] Object released.
这与自动引用计数无关,因为它已关闭,有什么想法吗?太感谢了。
同样,如果 MyTestClass 是这样的,构造函数和析构函数将不会被调用:
@interface MyTestClass : NSObject
{
myCppTestClass * myCppInstance;
}
和日志:
2012-11-16 12:22:38.710 iOSVersion[87428:f803] Create an object.
2012-11-16 12:22:38.711 iOSVersion[87428:f803] Object created
2012-11-16 12:22:38.712 iOSVersion[87428:f803] Object released.