Sneakyness
Sneakyness

Reputation: 5403

Objective-C @interface/pointer clarification

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.

Upvotes: 1

Views: 525

Answers (2)

rein
rein

Reputation: 33445

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

Upvotes: 2

johnw188
johnw188

Reputation: 742

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.

Upvotes: 7

Related Questions