Actually, I don't see any reason why compiler shouldn't enforce making
a const data member static.
Have you considered that cx
might be initialized in the constructor with a value known at run-time - and varying between different instances of A
?
const
members make assignment to them impossible, but sometimes having a member that cannot change its initial value proves useful.
Definitely not the best example, but to illustrate the idea:
struct Multiplier
{
const int factor;
Multiplier(int factor) : factor(factor) {}
int operator()( int val ) const
{
return val * factor;
}
};
std::vector<int> vec{1, 2, 3};
std::vector<int> vec2;
int i;
std::cin >> i;
std::transform( std::begin(vec), std::end(vec),
std::back_inserter(vec2), Multiplier(i) );
// vec2 contains multiples of the values of vec