Calling this->get/this->set methods versus directly accesing member variables in C++

前端 未结 8 676
挽巷
挽巷 2021-01-18 18:21

Suppose I have a class Foo, with a private variable bar_ containing some state for Foo. If necessary, I may write public get/set metho

8条回答
  •  無奈伤痛
    2021-01-18 19:19

    It's better to use the get and set methods: It's cleaner, upholds DRY principles better (only do validation once), and allows subclasses to override the methods and see consistent behavior on all changes to the variable.

提交回复
热议问题