问题描述
我正在尝试在我的代码中使用尽可能少的内存.我尝试了两种将自定义类对象发送到方法的方法.我不确定这两种方法之间是否有任何区别.假设我有 2 个类,Class1 和 Class2,当然每个类都有自己的类变量和方法.
I'm trying to use as little memory as possible in my code. I've tried two ways of sending a custom class object to a method. I'm not sure if there is any difference between these two approaches. Say I have 2 classes, Class1 and Class2 each with their own class variables and methods of course.
所有代码都写在Class1
方法 1:
Class2 *class2Object = [[Class2 alloc] init];
[self doSomething: class2Object];
[class2Object release];
-(void) doSomething: (Class2 *) var {
int a = var.a;
}
方法 2:
Class2 *class2Object = [[Class2 alloc] init];
[self doSomething: &class2Object];
[class2Object release];
-(void) doSomething: (Class2 **) var {
int a = var->a;
}
这两种方法在性能上有什么区别吗?第二种方法完全没有意义吗?为什么我可以在方法1中使用点表示法,而在方法2中必须使用->?
Is there any performance difference between these two methods? Is the second approach completely pointless? Why is it that I can use the dot notation in Approach 1, but have to use -> in Approach 2?
谢谢.
推荐答案
确实,性能上的差异可以忽略不计,因为在方法 2 中,您还有一个间接寻址(即指针取消引用,另见下文);因此,方法 1 将为您节省几个时钟周期.
indeed, there is a negligible difference in performance, due to the fact that in approach 2 you have one more indirection (i.e., pointer dereferencing, see also below); so, approach 1 will save you a few clock cycles.
第二种方法完全没有意义吗?
方法 2 很有用,例如,当您想分配 Class2 类型的新实例并通过相同的参数将其传回给调用者时;说:
approach 2 is useful,e.g., when you would like to allocate a new instance of type Class2 and pass it back to the caller through the same argument; say:
- (bool)cloneObject:(Class2 **)var;
你传入一个对象;对象被克隆并在 var 中返回;由于是对象本身的地址发生变化,因此您需要有一个指向对象指针的指针才能设置新地址;返回值仅说明操作是否正确执行.
you pass an object in; the object is cloned and returned in var; since it is the address of the object itself which changes, you need to have a pointer to the pointer to the object in order to set the new address; the return value only states if the operation was executed correctly.
当然,在这个例子中,这样做会更自然:
Of course, in this example, it would be more natural doing:
- (Class2)cloneObject:(Class2*)var;
即,您返回指向新分配对象的指针,但用例仍然成立.
i.e., you return the pointer to the object newly allocated, but the use case still holds.
为什么我可以在方法 1 中使用点表示法,而在方法 2 中必须使用 ->?
在第二种情况下,您必须使用 ->
因为您没有直接处理指向对象的指针;您正在处理指向对象指针的指针;在这种情况下,您需要做的是,首先,取消引用"您的指针(即,应用运算符 *)以获得指向对象的指针,然后像其他方式一样访问后者;这可以写成:
in the second case you have to use ->
because you are not dealing with a pointer to an object directly; you are dealing with a pointer to a pointer to an object; what you need do in such cases is, first of all, "dereferencing" your pointer (i.e., applying operator *) in order to get a pointer to the object, then accessing the latter as you would do otherwise; this could be written like:
(*var).a
这里,var
是一个指向Class2
对象的指针;*var
是解引用它的结果,所以你有一个指向 Class2
对象的指针;最后,.a
是访问对象属性的语法.语法:
here, var
is a pointer to a pointer to an object of Class2
; *var
is the result of dereferencing it, so you have a pointer to an object of Class2
; finally, .a
is the syntax to access an object property. the syntax:
var->a
只是上述操作的简写".
is simply a "shorthand" for the operations described above.
这篇关于点符号和 -> 之间的区别在目标 C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!