What\'s the difference between the types of bar1
and bar2
?
int foo = 10;
auto bar1 = &foo;
auto *bar2 = &foo;
In this specific example both bar1
and bar2
are the same. It's a matter of personal preference though I'd say that bar2
is easier to read.
However, this does not hold true for references as seen in this example:
#include
using namespace std;
int main() {
int k = 10;
int& foo = k;
auto bar = foo; //value of foo is copied and loses reference qualifier!
bar = 5; //foo / k won't be 5
cout << "bar : " << bar << " foo : " << foo << " k : " << k << endl;
auto& ref = foo;
ref = 5; // foo / k will be 5
cout << "bar : " << bar << " foo : " << foo << " k : " << k;
return 0;
}