-1

What is the difference between

Simple_window*  sw

and

Simple_window  *sw

where simple window is just class and sw is an object created from that class.

Philipp
  • 48,066
  • 12
  • 84
  • 109
hacked009
  • 35
  • 1
  • 9

2 Answers2

1

There is no difference based on the position of the * character.

Some people will say that

Simple_window* sw

is superior because it associates the pointer indicator * with the typename Simple_window to give the real type: Simple_window*, i.e. "Pointer to Simple_window".

Other people say that it is better to put the * close-up against the variables, since C++ will interpret it only for the next variable. That is,

Simple_window* sw, anotherSw

actually declares sw as a pointer, and anotherSw as a non-pointer Simple_window object! Because of this, the close-against-variable version might better indicate intent when using multiple declarations.

Simple_window *sw, *anotherSw

Because of this issue, I make it a habit not to use single-line declarations of multiple objects.

I prefer the first version, agreeing with the description I once read that it is more "C++-like".

NicholasM
  • 4,557
  • 1
  • 20
  • 47
  • No *semantical* difference, at least. There *is* stylistic difference, and arguably the one can be preferred over the other. –  Nov 23 '13 at 23:15
0

There isn't much difference except

Simple_window*  sw

means you are defining a variable of the type Simple_window* whereas

Simple_window  *sw 

means you are defining a pointer of the type Simple_window, both of them basically are pointers.

The difference arises, when you are defining multiple variables together, for example:

int* x, y, z;

implies that x, y and z are all pointers, which they are not (only x is). The second version does not have this problem:

int *x, y, z;

In other words, since the * binds to the variable name and not the type, it makes sense to place it right next to the variable name.

Ranveer
  • 6,683
  • 8
  • 45
  • 92