Contrived example, for the sake of the question:
void MyClass::MyFunction( int x ) const
{
std::cout << m_map[x] << std::endl
}
This won't compile, since the [] operator is non-const.
This is unfortunate, since the [] syntax looks very clean. Instead, I have to do something like this:
void MyClass::MyFunction( int x ) const
{
MyMap iter = m_map.find(x);
std::cout << iter->second << std::endl
}
This has always bugged me. Why is the [] operator non-const?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…