Const to access a database or file system

I regularly use const when working with data structures in memory and save my const-correct code, but I'm not sure how const should be applied to more complex objects, such as:

  • Objects representing connections to remote systems.
  • objects supported by the database (which can load components from the database on demand)
  • supported by a directory tree on disk (with access to a directory tree managed by a separate hierarchy of objects)

What should const methods mean for such objects? I can imagine a couple of possibilities:

  • "strict" const - methods that do not change state in memory are constants. However, this would seem to violate encapsulation, as this will require subscribers to find out which methods change the state of the connection and which do not.
  • "logical" const - methods that do not change the logical state of an object are constants. However, this would potentially require labeling a large number of state variables and caching as mutable . Although I understand that for mutable intended to be used, it is very similar to a hack. Also, given that const means “I promise not to change this,” it seems wrong to apply it when the methods can change the state of the connection in strange and wonderful ways (provided that they support encapsulation), the cache results as they want, throw exceptions if the connection fails, etc.
  • No const - In light of the previous problems, does const make little sense for more complex objects?

Which approach is most useful? What is the most common?

+7
source share
1 answer

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.

+2
source

All Articles