C++: Modifying array values for const -
consider following:
int a[2]; cin >> a[0] >> a[1]; const int d = a[1] - a[0]; cout << d << "\n"; a[1] = 5; a[0] = 2; cout << d << "\n";
i'm bit confused now. why print same value d? why doesn't changing array values change value of d? @ point in time value of d determined , stored?
thanks!
the value of d
determined in line assign it. can't change because values in expression used calculate changes, if weren't declared const
. (few programming languages have variables work excel spreadsheet cells: cost of recomputing unpredictable significant.)
if want value of d
change, have explicitly assign new value, compiler won't let unless remove const
.
Comments
Post a Comment