-5

So, I got a 2 variables declared in a class, let´s call it model.h and in this class i initialize instances of the classes Car and Road. If I want the Car to know about road what other options are there than passing a reference of Road to Car in Model that is good in this case?

Caleb
  • 124,013
  • 19
  • 183
  • 272
Tom Lilletveit
  • 1,872
  • 3
  • 31
  • 57
  • `.h` is the file name extension that indicates a header file -- you wouldn't name actual classes `Car.h` or `Road.h`. – Caleb May 12 '13 at 00:56
  • Caleb: It´s for clarity - as class usually got a header an implementation file – Tom Lilletveit May 12 '13 at 01:00
  • There's really no need -- just use the class name. – Caleb May 12 '13 at 01:06
  • 2
    @TomLilletveit You are actually making this harder to follow, not easier. – rmaddy May 12 '13 at 01:07
  • How is this a question about passing by reference (I don't see any code)? Do you mean "Alternatives for creating a circular reference" via header files? – Aaron May 12 '13 at 01:10
  • 2
    Is this about passing instances or including headers? You seem possibly a little bit unclear on the difference. – Chuck May 12 '13 at 01:14

3 Answers3

1

As I understand it, passing pointer to a pointer is not a great design pattern to use throughout your app. It's reserved in objective-c for only select few operations, namely NSError. See bbum's response in the thread below.

Arguments by reference in Objective-C

Judging by its acceptance and it's author I'd say it's pretty good advice, though perhaps not "gospel". You could certainly pass pointers to pointers all you want, but it's probably not the best approach.

The alternative is to consider what values you're considering passing by reference (meaning pointer-to-a-pointer) and then think about the design of the methods/functions so that you might encapsulate those values into a class that is returned from your method/function.

Community
  • 1
  • 1
Aaron
  • 7,055
  • 2
  • 38
  • 53
  • 1
    I wouldn't consider passing by reference to mean 'pointer-to-a-pointer'; in normal usage it means passing in a pointer. Any Obj-C class is passed by reference, although there are rare cases where you want to modify the reference, like the `NSError` one you've identified. – sapi May 12 '13 at 02:04
  • I think this is what the OP means, but I'm not certain. – Aaron May 12 '13 at 05:01
  • @sapi, you're right. The semantics are little confusing but I did mean "pointer to a pointer" and not passing by reference. I understand that obj-c objects are always passed by reference. I've amended my answer. – Aaron May 12 '13 at 05:41
1

In Objective-C, objects are always passed by reference, but you can mitigate that by carefully designing your classes.

For example, NSString and NSNumber are always passed by reference, but you can treat them as if they're passed by value because they're immutable. You can't alter an NSString (only an NSMutableString) or NSNumber, so it doesn't really matter whether you're passing by value or by reference.

Similarly, copying objects and using -isEqual: to compare them later can simulate pass-by-value. NSDictionary does this—-setObject:forKey: copies the key and uses only the copy, while -objectForKey: uses -isEqual: and -hash to compare keys.

In your case, you might be able to make Road objects immutable (or at least never be mutated after they're loaded) if your "map" of roads is fixed and you don't need to link roads back to cars. If you do that, then passing by reference is effectively a helpful optimization, not a bug waiting to happen.

Becca Royal-Gordon
  • 17,541
  • 7
  • 56
  • 91
0

There really are no other options. Objects are always accessed via pointers in Objective-C. There's no way to declare a variable that is an object, you can only create variables that refer to objects.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I was thinking if there where alternatives like how I design my classes like using a Singelton etc.. but singelton is not suitable in this case. – Tom Lilletveit May 12 '13 at 01:16