The addition of
const
ensures you will get compilation errors if you try to change the value of the referenced value inside the loop, is all. If they values shouldn't change, or you are passing them to a function that expects a const value, it makes sense.
But your code won't compile:
aNum
and
aNUm
are different variables!
With that corrected, in the specific case of your code as shown, you could use either:
int somenum[] = {1, -101, -1, 40, 2040 };
for (const int& aNum : somenum)
cout << aNum << endl;
Or
int somenum[] = {1, -101, -1, 40, 2040 };
for (int aNum : somenum)
cout << aNum << endl;
Because you aren't passing it to anything that would modify it.
There is a lot more detail here:
c++ - int vs const int& - Stack Overflow[
^]