我不熟悉objective c++,这是我的问题:假设我有一堂课
class MyTestClass
{
public:
int memLength_;
char *mem;
MyTestClass(int memLength)
{
memLength_ = memLength;
mem = new char[memLength_];
}
~MyTestClass()
{
delete []mem;
}
}
现在我要做的是MyTestClass
使用Objective C++创建一个对象,我所做的如下:
@interface ObjectRetriever : NSObject
+ (MyTestClass)ObtainMyTestClass; // Function 1
+ (MyTestClass*)ObtainMyNextTestClass; //Function 2
@end
+(MyTestClass)ObtainMyTestClass
{
int memLen = 100;
MyTestClass myClass(memLen)
return myClass;
}
+ (MyTestClass*)ObtainMyNextTestClass
{
MyTestClass *myClassPtr;
myClassPtr = new MyTestClass(memLen)
return myClassPtr;
}
所以当我使用ObjectRetriever
创建MyTestClass
对象时,我会这样做
MyTestClass testClassObject1 = [ObjectRetriever ObtainMyTestClass];
MyTestClass *testClassObjectPtr2 = [ObjectRetriever ObtainMyNextTestClass];
那么我的问题如下:
ObtainMyTestClass
并且ObtainMyNextTestClass
,哪个更好?- 如果我这样实现和使用类,会不会有内存泄漏的危险?
谢谢。