I’m adding some lazy initialization logic to a const method, which makes the method in fact not const. Is there a way for me to do this without having to remove the ‘const’ from the public interface?
int MyClass::GetSomeInt() const { // lazy logic if (m_bFirstTime) { m_bFirstTime = false; Do something once } return some int... }
EDIT: Does the ‘mutable’ keyword play a role here?
Make m_bFirstTime mutable:
…but this is also very often an indication of a design flaw. So beware.