Move constructor and const member variables

前端 未结 5 1062
自闭症患者
自闭症患者 2020-12-30 21:24

I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid

5条回答
  •  醉酒成梦
    2020-12-30 21:40

    This is why you should not declare said member variables const. const member variables usually serve no purpose. If you don't want users to mutate the FILE*, then don't provide them with functions to do that, and if you want to stop yourself from mutating it by accident, then mark your functions const. However, do not make member variables themselves const - because then you run into fun when you start to use move or copy semantics.

提交回复
热议问题