Can anyone tell me what the difference is between:
Display *disp = new Display();
and
Display *disp;
disp = new Display();
You have found four ways to write the same thing.
Examples 1 (Display *disp…
) and 3 (Display* disp…
) are identical; the spacing around *
does not matter. However, style 1 is often preferred, because:
Display* disp1, disp2;
actually means:
Display *disp1, disp2;
i.e., disp2 is not a pointer.
Example two (splitting across two lines) has the same effect, and will probably be compiled to the same code. The fourth example, using initializer syntax, does the same thing as well.
Note that if these were classes, not pointers, there could be a difference in behavior and speed.