1

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.

1
  • I'd also like to mention that out of the books I have, none of them take any time at all to clarify this. Commented Jul 25, 2009 at 19:16

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Alright, I figured as much for the primitives part, but the side the star is makes NO difference whatsoever?
Apparently not. Well this explains why I was so goddamn confused over nothing.
No it makes no difference at all.
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.
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.)
|
2

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

1 Comment

It really is impossible to google. I cannot convey how angry I was getting at my inability to figure this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.