Is there a performance difference between the two methods?
indeed, a slight difference in performance, due to the fact that in approach 2 you have another indirectness (i.e. dereferencing a pointer, see also below); therefore, approach 1 will save you a few measures.
Is the second approach completely pointless?
approach 2 is useful, for example, when you want to allocate a new instance of type Class2 and pass it back to the caller through the same argument; they say:
- (bool)cloneObject:(Class2 **)var;
you are passing an object; the object is cloned and returned to var; since this is the address of the object itself that is changing, you need to have a pointer to a pointer to the object in order to set a new address; the return value indicates only the correct operation.
Of course, in this example it would be more natural to do:
- (Class2)cloneObject:(Class2*)var;
ie, you are returning a pointer to the selected object, but the use case is still preserved.
Why can I use dot notation in approach 1, but I need to use โ in approach 2?
in the second case, you need to use -> because you do not have a direct pointer to the object; you are dealing with a pointer to a pointer to an object; what you need to do in such cases is, first of all, โdereferencingโ your pointer (that is, using the * operator) to get a pointer to the object and then access the latter, as you would otherwise; it can be written like this:
(*var).a
here, var is a pointer to a pointer to a Class2 object; *var is the result of dereferencing it, so you have a pointer to a Class2 object; finally, .a is the syntax for accessing an object property. syntax:
var->a
is simply an "abbreviation" for the operations described above.