views:

250

answers:

2

Learning as always, was going along quite nicely, until I realized I had no idea what the differences meant between these.

@class Player;
@class Map;

@interface View : NSView
{
    Player* player_;
    Map* currentMap_;
    NSMutableArray *worldArray;
    NSMutableArray *itemArray;
    float cellHeight_;
}

@end

Never mind, turns out the side the star is on has no effect at all. Now I know why I was so confused.

+6  A: 

All objective C objects are referenced by pointers, which is what the * denotes. Whether the star is on the left or the right doesn't matter to the compiler; I believe it's personal preference.

float doesn't have a * because it's a C primitive, not an Objective C object.

johnw188
Alright, I figured as much for the primitives part, but the side the star is makes NO difference whatsoever?
Sneakyness
Apparently not. Well this explains why I was so goddamn confused over nothing.
Sneakyness
No it makes no difference at all.
Perspx
Sneakyness: C is a free-format language. Whitespace doesn't matter except where it would be ambiguous without it, and even then, it doesn't matter how much or what whitespace you use. In this case, the minimum is “`Player*player_;`”. Whitespace makes things easier to read, and usually clearer, but is almost always not necessary to the compiler.
Peter Hosey
To wit: `int main(void){return!printf("%s%c%s\n","Hello",0x20,"world");}` Only one whitespace character in this entire valid program. (This is, of course, a bad way to write real code.)
Peter Hosey
Nope it's the best way ever and it's now how I'm writing all of my code.
Sneakyness
XCode's find and replace came in handy. No more spaces! ;P
Sneakyness
+1  A: 

It doesn't make a difference at all to the compiler. It's totally the preference of the developer.

The following are the same:

Player* player_;
Player *player_;
Player * player_;

There was an interesting comment I read once about the thought process of someone that types:

Player* player_;

vs that of someone that types:

Player *player_;

I can't find it now since this sort of stuff is impossible to google. The basic idea is that the developer who types Player* is thinking that player_ is a pointer to a Player object. The person who types it the other way is thinking that a Player object is contained in the dereferenced player_ variable. A subtle difference but ultimately the same thing.

One thing you might want to look out for is when creating multiple pointer variables in one line:

int *p, q;   // p is int*, q is int
int* p, q;   // not so obvious here, but p is int*, q is int
int *p, *q;  // it's a lot more obvious with the * next to the variable
rein
It really is impossible to google. I cannot convey how angry I was getting at my inability to figure this out.
Sneakyness