Saturday, September 25, 2010

Objective-C appropriate use of autorelease?

Programmer Question

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.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails