我正在学习 Objective-C,并且正在尝试编写一个 Vector3D 类。
在 C++ 或 Java 中,我想要实现的函数看起来像这样:
Vector3D
{
Vector3D crossProduct(Vector3D in)
{
Vector3D result;
result = ... // maths involving this classes variables and the input Vectors variables.
return result;
}
}
现在在 Objective-C 中,我无法返回对象,只能返回对象指针。这是使用自动释放的合适位置(在返回之前?)。这将使我能够链接 Vector3D 类上的方法(因为许多方法返回对象本身),但是与手动分配和释放对象相比,这会特别低效吗? Objective-C 中还有其他常用的返回对象的模式吗?
我想对班级进行的操作示例:
Vector3D * vector3D1 = [[Vector3D alloc] init];
Vector3D * vector3D2 = [[Vector3D alloc] init];
Vector3D * vector3D3 = [vector3D1 cross [ [vector3D2 normalize ] multiply 1.43f]];
谢谢。
I'm learning Objective-C and I'm trying to write a Vector3D class.
In C++ or Java the function I want to implement would look something like this:
Vector3D
{
Vector3D crossProduct(Vector3D in)
{
Vector3D result;
result = ... // maths involving this classes variables and the input Vectors variables.
return result;
}
}
Now in objective-c I can't return an object, only an object pointer. Would this be an appropriate place to use autorelease (before returning?). This would enable me to chain the methods on the Vector3D class (since many methods return the object itself) but would this be particularly inefficient as compared to manually allocating and releasing the objects? Are there other commonly used patterns used to return objects in objective-c?
example of operations I'd like to do with the class:
Vector3D * vector3D1 = [[Vector3D alloc] init];
Vector3D * vector3D2 = [[Vector3D alloc] init];
Vector3D * vector3D3 = [vector3D1 cross [ [vector3D2 normalize ] multiply 1.43f]];
Thanks.
发布评论
评论(2)
是的,这正是
autorelease
的正确用法,事实上,大多数使用您的代码的人都会完全期望cross
、normalize
和>multiply
返回一个自动释放的对象。Yes, this is exactly an appropriate usage of
autorelease
and in fact most people using your code would fully expect thatcross
,normalize
, andmultiply
return an autoreleased object.如果您创建了一个对象,您就“拥有”它。如果您想放弃或转移所有权,则必须自动释放或释放该对象。由于您无法释放一个对象然后返回它,因此您必须自动释放它。请注意,这比“适当使用”要强一些。
如果您未能执行此操作,静态分析器会警告您,请确保已将其打开。
你不应该向我学习内存管理规则,你应该向苹果学习内存管理规则。 http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH
Cocoa 没有 2d Point 类,而是频繁使用 C 结构(NSPoint 或 CGPoint),这可能是因为效率低下。部分是因为 Apple 的示例,部分是因为担心效率低下,您不会经常看到 3dVector 类。更常见的是,您会看到带有 4 个浮点的 C 结构体。这也可能是因为它对于 OpenGL 或 OpenCL 更有用。
只是简单地说,你说“在 Objective-C 中我不能返回一个对象,只能返回一个对象指针”。这与 Java 和 C++ 中的完全相同,并且并不是您在 Objective-C 中采取不同做法的原因。
If you create an object, you 'own' it. If you want to relinquish or transfer ownership you must autorelease or release the object. As you can't release an object and then return it, You must autorelease it. Note that this is somewhat stronger than being 'appropriate use'.
The static analyser will warn you if you fail to do this, make sure you have it turned on.
You should not learn the memory management rules from me, you should learn the memory management rules from apple. http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH
Cocoa doesn't have a 2d Point class, instead it makes frequent use of a C struct (NSPoint or CGPoint) instead, maybe because of inefficiencies. Partly because of Apple's example and partly because of fears of inneficiency, you won't very often see a 3dVector class. More commonly you will see a C struct with 4 floats instead. This might also be because it is more useful with OpenGL or OpenCL.
Just a quick point, you say "in objective-c I can't return an object, only an object pointer". This is exactly the same as in Java and C++, and isn't a reason why you would do things differently in objective-c.