Ilya Ilin
Ilya Ilin

Reputation: 2401

(C++ pointers) difference between var declarations

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?

Upvotes: 0

Views: 141

Answers (3)

Agentlien
Agentlien

Reputation: 5126

There is no semantic difference between your three suggestions.

Essentially, When a code file is parsed, the preprocessor splits everything into tokens, and all later passes of the compilation process works exclusively on these tokens. Thus, spacing between the tokens is completely ignored.

The syntax for a simple pointer declaration (with a single element being declared and without initialization) consists of three tokens: a type specifier, an asterisk (which denotes that the variable is a pointer) and, finally, an identifier. Thus, you can have any amount of whitespaces between these three, without changing the semantics at all.

The reason people choose one way over another has to do with style and what it expresses about intent.

MyClass* myVar makes it clear that the type of myVar is MyClass. MyClass *myVar makes it clear that the pointer token belongs to the myVar variable.

The second alternative is thus especially recommended if you decide to declare a list of variables in a single statement. Such as MyClass myVar, *myPointerToVar.

Another good alternative is to always use the MyClass* myVar variant, and never make a declaration listing several variables where some are pointers.

Upvotes: 1

stinky472
stinky472

Reputation: 6797

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:

int *ptr1, *ptr2;
...

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:

int* ptr = ...;

The rationale for that preference probably comes down to a couple of things:

  1. In C++, we have templates which require us to specify types on their own. vector<int*> seems a bit more straightforward about emphasizing int* as a single type rather than vector<int *> or some other variant.

  2. 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). *

    • This is quite different from C where every variable is required to be declared at the top of the scope which causes many C developers to have the frequent habit of declaring multiple at a time.

Upvotes: 2

hmjd
hmjd

Reputation: 122001

There is no difference between any of the declarations MyClass* myVar, MyClass * myVar or MyClass *myVar: it is a programmer preference.

Many programmers prefer MyClass *myVar, as it prevents making the following simple mistake (if the programmer intended to declare two pointers to MyClass):

MyClass* myVar, myVar2;

where myVar2 is not a MyClass*, but a MyClass.

Upvotes: 2

Related Questions