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.
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".
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.