What’s the difference between the variable declarations:
//version 1
MyClass* myVar = [[MyClass alloc] init];
//version 2
MyClass * myVar = [[MyClass alloc] init];
//version 3
MyClass *myVar = [[MyClass alloc] init];
what changes will occur with the object myVar for each version?
Purely stylistic, no difference. In C, developers tend to prefer the latter style, probably for the simple reason that it makes declaring multiple pointers clearer:
It’s also the style of the original, popular authors when C came out like Kernighan and Ritchie’s C Programming Language. Dennis Ritchie, by the way, created C.
However, a lot of modern C++ developers, including Stroustrup himself (creator of C++), tend to favor that first convention:
The rationale for that preference probably comes down to a couple of things:
In C++, we have templates which require us to specify types on their own.
vector<int*>seems a bit more straightforward about emphasizingint*as a single type rather thanvector<int *>or some other variant.When adhering to C++ coding standards which are intended to promote safe designs, we don’t find ourselves wanting to define multiple variables at once so often since we generally want to define them when they can be meaningfully initialized (avoiding potential errors by limiting scope and immediately initializing them). *