It's a difficult question. I am working on the same thing now. I have a large in-memory data structure supported by a MySQL database. I started with the const where possible possible approach, but in practice this happens infrequently:
- Perhaps you are using a third-party library that is not constant.
- Not all
SELECT
are actually read-only. For example, SELECT GET_LOCK
definitely changes state on the db side. - I have not met many C ++ programmers who can use
mutable
correctly. Of course, I don’t have much experience. Future attendants may cause more problems than volatile. You know your team better, so it's up to you whether you want to go this route.
I think that a const object with a single mutable database descriptor might be the most elegant solution. Decide what the const object should reasonably execute, and make all these const functions. Users in your class only need to know which functions are constants and which are not. The header file will give them this. They do not need to know why a particular function is constant or not constant. I agree that a bunch of mutable members are starting to look like hackers.
In my case, I basically had to give up const-correctness. The const object would be so handcuffed that it could not bring anything. Based on my many years of experience, I suspect that this usually happens in practice. The logical constant-correctness is a good ideal (and you should definitely start there if you build from scratch), but when it comes to the real delivery code, no const is suitable.
Michael Kristofik
source share