Henry Barker Henry Barker - 5 days ago 6
C++ Question

C++11 auto declaration with and without pointer declarator

What's the difference between the types of

bar1
and
bar2
?

int foo = 10;
auto bar1 = &foo;
auto *bar2 = &foo;


If both
bar1
and
bar2
are
int*
, does it makes sense to write the pointer declarator (
*
) in the
bar2
declaration?

Answer

The declarations are exactly equivalent. auto works (almost) the same as template type deduction. Putting the star explicitly makes the code a bit easier to read, and makes the programmer aware that bar2 is a pointer.

Comments